問題描述
我正在用 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、Version=4.0.0.0、Culture=neutral、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 is not defined"之類的錯誤(但它在我啟動時仍然運行!).
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 AssembliesMicrosoftFramework.NETFrameworkv4.5.1FacadesSystem.Runtime.dll
PS:如果您只有 Windows 10 桌面應用程序,您可能希望使用新的 Toast 系統,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 :
例外是因為您需要在 CreateToastNotifier()
ToastNotificationManager.CreateToastNotifier("MyApplicationId").Show(toast);
它是將在操作中心用于對您的 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()
.
這篇關于顯示 Windows 10 Toast 通知的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!