显示来自 Windows 窗体应用程序的 Windows 8 Toast [英] Showing Windows 8 toast from Windows Forms App

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

问题描述

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

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.

是否可以从这个 WinForm 应用程序中为运行 Windows 8 的用户显示 Windows 8 Toast 通知(Windows.UI.Notifications 命名空间)?

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

我似乎找不到任何示例.我发现的一切都是 WPF 或 Windows 应用商店应用程序——没有任何示例是 WinForm 应用程序.

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

推荐答案

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

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.

在桌面项目中,Core 选项卡默认不显示.您可以通过右键单击解决方案资源管理器中的项目,选择卸载项目,添加以下代码段,然后重新打开项目(右键单击项目选择重新加载项目)来添加 Windows 运行时.当您打开参考管理器"对话框时,会出现核心"选项卡.然后你可以添加Windows"参考项目.

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)

在C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\Facades\System.Runtime.dll"中手动添加dll(右键引用,添加引用,浏览)

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)

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

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天全站免登陆