微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 显示Windows 10 Toast通知

我正在用C#(Visual Studio 2015)开发一个程序,我想在某种情况下向用户显示一个toast消息.我从MSDN下载了这段代码,运行正常:
// 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);

测试完这段代码后,我想将它实现到我的应用程序中.所以我稍微改了一下并尝试运行它.错误消息:

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未定义”等等(但是当我启动它时它仍会运行!).

我能想到的唯一可能的解决方案是System.Runtime已经安装在我的计算机上的某个地方,并且4.0.0是我程序的错误版本.但我无法在任何地方找到它.

PS:这是桌面应用程序,而不是Windows应用程序.

解决方法

我认为这与 this question中的问题相同
您必须添加引用
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\Facades\System.Runtime.dll

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

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

例外是因为您需要在CreatetoastNotifier()中提供applicationId

Toastnotificationmanager.CreatetoastNotifier("MyApplicationId").Show(toast);

这是将在动作中心用于分组祝酒词的名称(通常,您将应用程序的名称设置为).在Windows 8.1中,需要注册您的应用程序ID(我认为这是来自MSDN的示例),但现在您只需输入应用程序的名称即可.

GetXml()仅适用于WinRT.在桌面版中,您需要像使用GetContent()一样.

原文地址:https://www.jb51.cc/csharp/98532.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐