窗10:显示Toast通知 [英] Windows 10: Showing a toast Notification

查看:565
本文介绍了窗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:

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"

(翻译)

也是一样的的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/
后,该错误走了,但现在literaly的我的代码中每一个字cringled红色,像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商店应用程序

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只有桌面应用程序,你可能希望在使用新的土司制度,代码示例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 :

唯一的例外是,因为你需要提供一个的applicationID CreateToastNotifier()

The exception is because you need to provide an applicationId in 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()仅适用于的 WinRT的。在桌面上,你需要做的像你这样的的getContent()

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

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

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