从 Windows 窗体应用程序显示 Windows 8 toast [英] Showing Windows 8 toast from Windows Forms App

查看:15
本文介绍了从 Windows 窗体应用程序显示 Windows 8 toast的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

解决方案

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 AssembliesMicrosoftFramework.NETFrameworkv4.5FacadesSystem.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);

这篇关于从 Windows 窗体应用程序显示 Windows 8 toast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆