我正在开发C#(Visual Studio 2015)中的程序,我想在某种情况下向用户显示Toast消息。我从网上下载MSDN这个代码,它运行的罚款:显示Windows 10 Toast通知
// 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);
测试这个代码,我想落实到我的应用程序后。所以我改变了一点,试图运行它。错误消息:
类型“IReadOnlyList <>”在未引用的程序集中定义。添加一个参考System.Runtime,版本= 4.0.0.0,文化=中性公钥= 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 每我的代码中的单词是红色的,像“System.Object未定义”等错误(但它仍然在我启动时运行!)。
我能想到的唯一可能的解决方案是System.Runtime已经安装在我的电脑的某个地方,而4.0.0是我的程序的错误版本。但我无法在任何地方找到它。
PS:这是一个桌面应用程序,而不是Windows Store应用程序。
在错误出现之前,您发生了什么变化?因为我理解你的问题,如果你不改变任何东西,MSDN的代码工作正常吗? –