Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing Windows 8 toast from Windows Forms App

We've got a Windows Forms Application (vs. WPF) that was originally created for Windows 7. We're carrying the product forward to be used in Windows 8.

Is it possible to show Windows 8 Toast Notifications (Windows.UI.Notifications namespace) from this WinForm app, for users running Windows 8?

I can't seem to find any examples. Everything I find is a WPF or Windows Store apps—no examples are WinForm apps.

like image 783
core Avatar asked May 10 '13 16:05

core


People also ask

What is a Windows Toast notification?

A toast notification is a message that your app can construct and deliver to your user while they are not currently inside your app. This quickstart walks you through the steps to create, deliver, and display a Windows 10 or Windows 11 toast notification using rich content and interactive actions.

How do I send toast notifications?

To send a text alert from a Toast POS device: 1. Select the overflow icon ( ⋮) on the top right corner of the device screen and select Send Text Alert.

How do I create a Windows notification?

To get started, head to Settings > System > Notifications & actions‌ – or, if you're on a Windows 10 PC, click here to open notifications & actions. First, send notifications, reminders and alarms directly to the action center by right-clicking action center in your taskbar, then selecting Turn on quiet hours.


1 Answers

It is possible to use toast notification in winform poject under win 8. I create a winform project, add a button, when pressing the button, there will be a toast notification showed in the right-top of the window. The following is what I have done.

First, you need to open win 8 platform by modifying the cspoj file(default closed in winform project), so that you can add "Windows" reference.

In the desktop projects the Core tab doesn’t appear by default. You can add the Windows Runtime by right click the project in Solution Explore, choosing Unload Project, adding the following snippet, and re-opening the project (right click project choose Reload Project). When you open the Reference Manager dialog box, the Core tab appears. Then you can add "Windows" reference to the project.

<PropertyGroup>
      <TargetPlatformVersion>8.0</TargetPlatformVersion>
</PropertyGroup>

For more details you can refer to this link(near to the end part of the page)

Second, add System.Runtime reference.

Manually add the dll in "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\Facades\System.Runtime.dll" (right click Reference, add Reference, Browse)

Third, add toast notification (you can add this codes to a button-pressed event)

This parts of codes you can refer to this link Notes: if you only want to show toast notification, you don't need to care about ShellHelpers.cs. Or if you like, you can just copy the codes below.You may need to add some usings accordingly, and there maybe a picture, if don't have, it can still run. Oh, you also need to set a APP_ID (just a const string to represent uniqueness).

private const String APP_ID = "Microsoft.Samples.DesktopToastsSample";

using Windows.UI.Notifications;
using Windows.Data.Xml.Dom;
using System.IO;


// Get a toast XML template
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);

// Fill in the text elements
Windows.Data.Xml.Dom.XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
for (int i = 0; i < stringElements.Length; i++)
{
    stringElements[i].AppendChild(toastXml.CreateTextNode("Line " + i));
}

// Specify the absolute path to an image
String imagePath = "file:///" + Path.GetFullPath("toastImageAndText.png");
Windows.Data.Xml.Dom.XmlNodeList imageElements = toastXml.GetElementsByTagName("image");
imageElements[0].Attributes.GetNamedItem("src").NodeValue = imagePath;

// Create the toast and attach event listeners
ToastNotification toast = new ToastNotification(toastXml);
//toast.Activated += ToastActivated;
//toast.Dismissed += ToastDismissed;
//toast.Failed += ToastFailed;

// Show the toast. Be sure to specify the AppUserModelId on your application's shortcut!
ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);
like image 108
Jensen Avatar answered Oct 10 '22 01:10

Jensen