Display a Windows 8 toast from a Windows Forms application

We have a Windows Forms application (vs. WPF), which was originally created for Windows 7. We are moving the product forward for use in Windows 8.

Can I show Windows 8 Toast Notifications (Windows.UI.Notifications namespace) from this WinForm application for users running Windows 8?

I can not find examples. All I find are WPF or Windows Store applications - WinForm application examples are not.

+6
windows-8 winforms toast
source share
1 answer

You can use the toast notification in winform poject under win 8. I create a winform project, add a button by clicking the button, a toast notification will appear in the upper right corner of the window. The following is what I did.

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

In projects on the desktop, the Core tab is not displayed by default. You can add Windows Runtime by right-clicking on a project in Solution Explore, selecting Unload Project, adding the following snippet, and reopening the project (right-click the project "Update Project"). When you open the Reference Manager dialog box, the Core tab appears. Then you can add the "Windows" link to the project.

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

For more information, you can refer to this link (near the end of the page)

Second, add the System.Runtime link.

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

Third, add a toast notification (you can add these codes to the event with the button pressed)

This part of the codes you can refer to this link Notes. If you want to show only toasts, you do not need to worry about ShellHelpers.cs. Or if you want, you can simply copy the codes below. You may need to add some of them accordingly, and maybe the image, if it doesn’t already, can still work. Oh, you also need to set APP_ID (just a constant 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 shortcut! ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast); 
+3
source share

All Articles