14

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:

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)

Same goes for IEnumerable<> and IReadOnlyList<>

The error come from these two lines:

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

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!).

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: It's a desktop-application, not a Windows-Store application.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Bobface
  • 2,782
  • 4
  • 24
  • 61
  • what did you change before the error appeared? as I understand your question the code from MSDN is working fine if you don't change anything? – Loreno Heer Aug 25 '15 at 21:59

1 Answers1

20

I think it is the same problem as in this question You must add a reference to

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

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 :

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

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

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.

And the GetXml() is only for WinRT. In desktop you need to do like you did with the GetContent().

Community
  • 1
  • 1
k94ll13nn3
  • 679
  • 7
  • 17
  • Thank you, the exception is gone. But now the problem is that only a standard Toast will be shown - the toast does not show my headline / body text. I'm using `Windows.Data.Xml.Dom.XmlDocument doc = new Windows.Data.Xml.Dom.XmlDocument(); doc.LoadXml(content.GetContent()); ToastNotificationManager.CreateToastNotifier("Bether").Show(toast);` The variable `content` is of type `ToastContent` - here I specified my own text etc. But it does not get shown - Only the stanard "new notification". – Bobface Aug 29 '15 at 22:37
  • 2
    @Bobface This problem often appears when the toast contains invalid data, especially images. Personally, I had trouble using relative path for image on my drive so I had to use the absolute path. – k94ll13nn3 Aug 30 '15 at 07:36