显示 Windows 10 Toast 通知 [英] Showing a Windows 10 toast notification

查看:34
本文介绍了显示 Windows 10 Toast 通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C# (Visual Studio 2015) 开发一个程序,我想在特定情况下向用户显示一条吐司消息.我从 MSDN 下载了这段代码,它运行良好:

I'm developing a program in C# (Visual Studio 2015) and I want to show a toast message to the user at a certain situation. I downloaded this code from the MSDN and it runs fine:

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

// Fill in the text elements
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");
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);

测试此代码后,我想将其实现到我的应用程序中.所以我稍微改变了它并尝试运行它.错误信息:

After testing this code I wanted to implement it into my application. So I changed it up a little bit and tried to run it. The error messages:

类型IReadOnlyList<>"是在未引用的程序集中定义的.添加对 System.Runtime、Version=4.0.0.0、Culture=neutral、PublicKeyToken=b03f5f7f11d50a3a 的引用"(已翻译)

The type "IReadOnlyList<>" is defined in a not referenced assembly. Add a reference to System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" (translated)

同样适用于 IEnumerable<>IReadOnlyList<>

错误来自这两行:

for (int i = 0; i < stringElements.Length; i++)
{
    stringElements[i].AppendChild(toastXml.CreateTextNode("Line " + i));

我还尝试添加对 System.Runtime 的引用.我用 NuGet (https://www.nuget.org/packages/System.Runtime/4.0.0/).在那之后,错误消失了,但现在我的代码中的每个字都变成了红色,并带有诸如System.Object is not defined"之类的错误(但它在我启动时仍然运行!).

I also tried adding the reference to System.Runtime. I downloaded it with NuGet (https://www.nuget.org/packages/System.Runtime/4.0.0/). After that the errors were gone, but now literaly every word in my code is cringled red with error like "System.Object is not defined" and so on (but it still runs when I start it!).

我能想到的唯一可能的解决方案是 System.Runtime 已经安装在我电脑的某个地方,而 4.0.0 是我程序的错误版本.但我在任何地方都找不到.

The only possible solution I can think of is that System.Runtime is already installed somewhere on my computer, and that 4.0.0 is the wrong version for my program. But I can't find it anywhere.

PS:它是桌面应用程序,而不是 Windows 商店应用程序.

PS: It's a desktop-application, not a Windows-Store application.

推荐答案

我觉得和 这个问题您必须添加对

I think it is the same problem as in this question You must add a reference to

C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5.1FacadesSystem.Runtime.dll

PS:如果您只有 Windows 10 桌面应用程序,您可能希望使用新的 Toast 系统,MSDN 上的代码示例使用的是 Windows 8 系统.它适用于 W10,但没有所有新功能(微软发布了官方 NuGet 包).

PS : If you have a Windows 10 only desktop app, you might want to use the new toast system, the code sample on MSDN uses the Windows 8 one. it works on W10 but does not have all the new features (Microsoft released an official NuGet package).

由于我无法发表评论,我将在此处发布答案:

Edit : Since I can't comment, I will post the answer here :

例外是因为您需要在 CreateToastNotifier()

ToastNotificationManager.CreateToastNotifier("MyApplicationId").Show(toast);

它是将在操作中心用于对您的 toast 进行分组的名称(因此通常,您输入应用程序的名称).在 Windows 8.1 中,需要注册您的应用程序 ID(我认为这在 MSDN 的示例中),但现在您只需输入应用程序的名称即可.

It is the name that will be used in the action center to group your toasts (so in general, you put the name of your app). In Windows 8.1 it was needed to register your application Id (I think this was in the sample from the MSDN) but now you can just put the name of your app.

GetXml() 仅适用于 WinRT.在桌面上,您需要像使用 GetContent() 那样做.

And the GetXml() is only for WinRT. In desktop you need to do like you did with the GetContent().

这篇关于显示 Windows 10 Toast 通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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