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); 测试完这段代码后,我想将它实现到我的应用程序中.所以我稍微改了一下并尝试运行它.错误消息:
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.Runtime已经安装在我的计算机上的某个地方,并且4.0.0是我程序的错误版本.但我无法在任何地方找到它. PS:这是桌面应用程序,而不是Windows应用程序. 解决方法
我认为这与
this question中的问题相同
您必须添加引用 C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5.1FacadesSystem.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()一样. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |