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

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

问题描述

我正在用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的引用,版本= 4.0.0.0,区域性=中性,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"之类的错误,依此类推(但在我启动它时它仍然会运行!) .

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 Store应用程序.

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 Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\Facades\System.Runtime.dll

PS:如果您拥有仅Windows 10的桌面应用程序,则可能要使用新的Toast系统,MSDN上的代码示例使用Windows 8.它可以在W10上使用,但没有所有新功能(Microsoft发布了官方

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);

此名称将在操作中心中用于对您的敬酒进行分组(因此,通常,您放置应用程序的名称).在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()仅适用于

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

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

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