从Windows Forms App显示Windows 8吐司

cor*_*ore 7 toast winforms windows-8

我们有一个最初为Windows 7创建的Windows窗体应用程序(与WPF相比).我们正在推出该产品,以便在Windows 8中使用.

对于运行Windows 8的用户,是否可以从此WinForm应用程序中显示Windows 8 Toast Notifications(Windows.UI.Notifications命名空间)?

我似乎无法找到任何例子.我发现的一切都是WPF或Windows应用商店应用 - 没有WinForm应用的例子.

Jen*_*sen 4

在win 8下的winform项目中可以使用toast通知。我创建一个winform项目,添加一个按钮,按下按钮时,窗口右上角会显示一个toast通知。以下是我所做的。

\n

首先,需要通过修改cspoj文件来打开win 8平台(winform项目中默认关闭),这样就可以添加“Windows”引用。

\n

在桌面项目中,默认情况下不会显示“核心”选项卡\xe2\x80\x99。您可以通过右键单击“解决方案资源管理器”中的项目,选择“卸载项目”,添加以下代码片段,然后重新打开该项目(右键单击项目选择“重新加载项目”)来添加 Windows 运行时。当您打开“参考管理器”对话框时,将出现“核心”选项卡。然后您可以将“Windows”引用添加到项目中。

\n
<PropertyGroup>\n      <TargetPlatformVersion>8.0</TargetPlatformVersion>\n</PropertyGroup>\n
Run Code Online (Sandbox Code Playgroud)\n

有关更多详细信息,您可以参考此链接(靠近页面末尾部分)

\n

其次,添加 System.Runtime 引用。

\n

手动添加“C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework.NETFramework\\v4.5\\Facades\\System.Runtime.dll”中的dll(右键Reference,添加Reference 、浏览)

\n

第三,添加 toast 通知(您可以将此代码添加到按钮按下事件中)

\n

这部分代码可以参考这个链接\n注:如果你只想显示toast通知,则不需要关心ShellHelpers.cs。或者如果你愿意,你可以复制下面的代码。你可能需要相应地添加一些用法,并且可能有图片,如果没有,它仍然可以运行。哦,你还需要设置一个APP_ID(只是一个const字符串来表示唯一性)。

\n
private const String APP_ID = "Microsoft.Samples.DesktopToastsSample";\n\nusing Windows.UI.Notifications;\nusing Windows.Data.Xml.Dom;\nusing System.IO;\n\n\n// Get a toast XML template\nXmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);\n\n// Fill in the text elements\nWindows.Data.Xml.Dom.XmlNodeList stringElements = toastXml.GetElementsByTagName("text");\nfor (int i = 0; i < stringElements.Length; i++)\n{\n    stringElements[i].AppendChild(toastXml.CreateTextNode("Line " + i));\n}\n\n// Specify the absolute path to an image\nString imagePath = "file:///" + Path.GetFullPath("toastImageAndText.png");\nWindows.Data.Xml.Dom.XmlNodeList imageElements = toastXml.GetElementsByTagName("image");\nimageElements[0].Attributes.GetNamedItem("src").NodeValue = imagePath;\n\n// Create the toast and attach event listeners\nToastNotification toast = new ToastNotification(toastXml);\n//toast.Activated += ToastActivated;\n//toast.Dismissed += ToastDismissed;\n//toast.Failed += ToastFailed;\n\n// Show the toast. Be sure to specify the AppUserModelId on your application\'s shortcut!\nToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);\n
Run Code Online (Sandbox Code Playgroud)\n