4

I wanted to add toast notifications to my wpf app.

I've followed the Send a local toast notification from desktop C# apps from Microsoft, but I'm stuck on the step 5.

I'm not sure how to make this code working:

// Construct the visuals of the toast (using Notifications library)
ToastContent toastContent = new ToastContentBuilder()
    .AddToastActivationInfo("action=viewConversation&conversationId=5", ToastActivationType.Foreground)
    .AddText("Hello world!")
    .GetToastContent();

// And create the toast notification
var toast = new ToastNotification(toastContent.GetXml());

// And then show it
DesktopNotificationManagerCompat.CreateToastNotifier().Show(toast);

Also, I've added <TargetPlatformVersion>10.0</TargetPlatformVersion> to the .csproj, references to Windows.Data, Windows.UI.

At this point, I was getting 2 errors:

The type 'XmlDocument' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows.Foundation.UniversalApiContract, Version=7.0.0.0, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'

The type 'ToastNotifier' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows.Foundation.UniversalApiContract, Version=7.0.0.0, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'

When I add Windows.Foundation.UniversalApiContract as a reference from C:\Program Files (x86)\Windows Kits\10\References\10.0.18362.0\Windows.Foundation.UniversalApiContract\8.0.0.0\Windows.Foundation.UniversalApiContract.winmd I get the following error:

The type 'ToastNotification' exists in both 'Windows.Foundation.UniversalApiContract, Version=8.0.0.0, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime' and 'Windows.UI, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'

How can I fix it?

Please note that I've also tried to use the 'ready example', but with the same results.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
sswwqqaa
  • 1,545
  • 3
  • 15
  • 29
  • You link to an article on UWP, while you tag your question WPF. Is this the reason your toasts aren't working? – Maciek Świszczowski Aug 23 '20 at 14:34
  • From the begging of the first link: `Desktop apps (including packaged MSIX apps, apps that use sparse packages to obtain package identity, and classic non-packaged Win32 apps) can send interactive toast notifications just like Windows apps.` I thought that like Windows apps refer to UWP and things like classic Win32 apps may refer to wpf. I might be wrong. Also, I'm pretty sure that it's possible to add toast notifications to wpf app in similar way. – sswwqqaa Aug 23 '20 at 14:47
  • 2
    There's a nuget package is a lot simpler to implement if you just want toast and don't care about the exact implementation. https://github.com/Federerer/Notifications.Wpf – Andy Aug 23 '20 at 14:49
  • As an another invented wheel, [here's](https://stackoverflow.com/a/61564124/12888024) some easy UI implementation for Toast messages. :) – aepot Aug 23 '20 at 14:55
  • Thanks @Andy, I'll probably end up using it. However, right now I'm looking for more direct solution like using toast from UWP. – sswwqqaa Aug 23 '20 at 15:01

2 Answers2

6

NEW:

There is a way of using UWP Toast Notifications in WPF app.

  1. Add <TargetPlatformVersion>10.0</TargetPlatformVersion> to .csproj
  2. If you have any package installed, click on packages.config file and select Migrate packages.config to PackageReference... (Important step - this was the thing that I was missing)
  3. Now in Package Manager Console install UWP.Notifications package e.g.: Install-Package Microsoft.Toolkit.Uwp.Notifications -Version 7.0.2 Check version

Now, you should be able to use all uwp notification functions.

OLD:

As @Andy suggested in the comments there is ready NuGet package implementation: github.com/Federerer/Notifications.Wpf

If you just want a simple notification without onClick events etc. you can use this solution:

  1. Add <TargetPlatformVersion>10.0</TargetPlatformVersion> to .csproj
  2. Add references to Windows.UI and Windows.Data
  3. Add following usings: using Windows.Data.Xml.Dom; using Windows.UI.Notifications;
  4. Use this code to show notification:
var message = "Sample message";
var xml = $"<?xml version=\"1.0\"?><toast><visual><binding template=\"ToastText01\"><text id=\"1\">{message}</text></binding></visual></toast>";
var toastXml = new XmlDocument();
toastXml.LoadXml(xml);
var toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier("Sample toast").Show(toast);

More info

sswwqqaa
  • 1,545
  • 3
  • 15
  • 29
  • Images don't show in this solution. Buttons and text show only. – Ali123 Jul 12 '21 at 08:07
  • You should be able to use all features of Uwp.Notifications. Can you share your code for the notification? – sswwqqaa Jul 13 '21 at 09:45
  • Note that there is no immediate UI once you run the app, it will show in the system tray. You can test it using the "Native Notification" button – Ali123 Jul 13 '21 at 11:39
  • 1
    Take a look at https://stackoverflow.com/questions/46246430/uwp-toast-works-but-images-adaptiveimage-toastgenericheroimage-toastgenerica Also, I'd suggest opening a new question (I'm not sure what might be causing this issue with images). – sswwqqaa Jul 13 '21 at 23:48
  • 1
    For .NET 5, change the target framework in the .csproj file to `net5.0-windows10.0.17763.0` or higher. – Bip901 Sep 19 '21 at 08:21
0

This documentation is very well made, link here.

Follow Desktop(unpackaged) for specific details on WPF apps.

gl3yn
  • 301
  • 1
  • 3
  • 13