'How can I integrate toast notifications in WPF app?
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.
Solution 1:[1]
NEW:
There is a way of using UWP Toast Notifications in WPF app.
- Add
<TargetPlatformVersion>10.0</TargetPlatformVersion>
to .csproj - If you have any package installed, click on
packages.config
file and selectMigrate packages.config to PackageReference...
(Important step - this was the thing that I was missing) - 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:
- Add
<TargetPlatformVersion>10.0</TargetPlatformVersion>
to .csproj - Add references to
Windows.UI
andWindows.Data
- Add following usings:
using Windows.Data.Xml.Dom; using Windows.UI.Notifications;
- 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);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Thomas Shephard |