Windows 10 Toast Notification Display

I am developing a program in C # (Visual Studio 2015), and I want to show a toast message to the user in a specific situation. I downloaded this code from MSDN and it works 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 shortcut! ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast); 

After testing this code, I wanted to implement it in my application. So I changed it a bit and tried to run it. Error messages:

The type "IReadOnlyList <>" is defined in the assembly without reference. Add a link to System.Runtime, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a "(Translation)

The same goes for IEnumerable<> and IReadOnlyList<>

The error occurs from these two lines:

 for (int i = 0; i < stringElements.Length; i++) { stringElements[i].AppendChild(toastXml.CreateTextNode("Line " + i)); 

I also tried adding a link to System.Runtime. I downloaded it using NuGet ( https://www.nuget.org/packages/System.Runtime/4.0.0/ ). After that, the errors disappeared, but now literally every word in my code crintled red with an error, for example, "System.Object is not defined", etc. (But it still works when I run it!),

The only possible solution I can think of is that System.Runtime is already installed somewhere on my computer, and 4.0.0 is the wrong version for my program. But I can’t find anything.

PS: This is a desktop application, not a Windows-Store application.

+5
source share
1 answer

I think the same problem as in this question you should add a link to

 C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\Facades\System.Runtime.dll 

PS: If you have only the Windows 10 desktop application, you can use the new system for toasts, the code sample in MSDN uses Windows 8. It works on W10, but does not have all the new features (Microsoft released the official NuGet package).

Edit: since I cannot comment, I will post the answer here:

The exception is that you need to provide applicationId in CreateToastNotifier()

 ToastNotificationManager.CreateToastNotifier("MyApplicationId").Show(toast); 

This is the name that will be used in the action center to group your toasts (so, in general, you put the name of your application). In Windows 8.1 it was necessary to register your application identifier (I think it was in the sample from MSDN), but now you can simply put the name of your application.

And GetXml() is for WinRT only. On the desktop, you need to do as you do with GetContent() .

+11
source

All Articles