8
votes

I'm trying to display Windows 10 Toasts with my WPF C# Desktop application.

Sadly the API and general support concerning Windows 10 notifications in non-UWP or Store apps seems pretty limited and chaotic. Lately the UWP Community Toolkit was published, which seems to try and make things easier for us. There's also this Store app, Notifications Visualizer, which helps in making Toasts like this:

enter image description here

I went on and tried to generated the Toast using C# and the API provided by the UWP Community Toolkit.

using Microsoft.Toolkit.Uwp.Notifications;

ToastContent toastContent = new ToastContent()
{
    Visual = new ToastVisual()
    {
        BindingGeneric = new ToastBindingGeneric()
        {
            Children =
            {
                new AdaptiveText()
                {
                    Text = "Matt sent you a friend request"
                },
                new AdaptiveText()
                {
                    Text = "Hey, wanna dress up as wizards and ride around on our hoverboards together?"
                }
            },
            AppLogoOverride = new ToastGenericAppLogo()
            {
                Source = "https://unsplash.it/64?image=1005",
                HintCrop = ToastGenericAppLogoCrop.Circle
            }
        }
    }
};


XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(toastContent.GetContent());

var toast = new ToastNotification(xmlDoc);
ToastNotificationManager.CreateToastNotifier(AppId).Show(toast); // Display toast

Unfortunately, no matter what I try, I seem to be unable to get the same result, the Image is always missing for some reason:

enter image description here

Most information I found concerning these notifications are either outdated or useless. Can someone please shed some light on this? Thank you.

1
The asker makes it pretty clear that this is a desktop app. Not sure if the UWP Community Toolkit namespaces are supposed to compile/run on old-school desktop apps, though, considering the toolkit is designed specifically for UWP.BoltClock♦
@BoltClock, I'm not familiar with UWP, but AFAIK this is essentially just XML code passed to the underlying notification API. Shouldn't this decouple the whole thing and make it almost irrelevant what kind of App is calling that function?r41n
I did a bit more research and it looks like Microsoft.Toolkit.Uwp.Notifications actually has libraries for .NET, not just WinRT/UWP. I'm assuming you're using the .NET Standard library. So much for "UWP Community Toolkit"... anyway, yes it should decouple the notification format from the API/platform. Unfortunately I haven't updated any of my WPF apps with support for the new notifications API, so I can't comment further.BoltClock♦
Actually I just took the Microsoft.Toolkit.Uwp.Notifications Nuget package since I couldn't find any library specifically marked as .NET Standard.r41n
When I try to use the Microsoft.Toolkit.Uwp.UI.Controls Nuget package I get an error telling me that there are no .NET Framework compatible items inside. Since the .Notification package installed fine I guess that means it also contains the .NET Standard libraries.r41n

1 Answers

10
votes

No way of doing this with the UWP Toolkit alone

It looks like with .NET Standard this can only be achieved in two steps, and one of them is out of the scope of the UWP Toolkit.

.NET Standard Apps require a COM Server and a special Start Menu Shortcut to properly use the Windows 10 Action Center. UWP Apps seem to not require or already come with an equivalent functionality. These two steps are supposed to be performed during application installation, and this is obviously something the Microsoft UWP Toolkit takes no part in. Thus the UWP Toolkit alone is not only unable but never will be able to provide a complete solution for displaying Windows 10 Toasts for .NET Standard in a self contained way.

Alternatives

I found an obscure C# Project on Github that has 'Microsoft' in its name, it works out of the box without the UWP Toolkit. It requires you to provide a GUID and an AppID string which then is used to register the COM Server and create the Shortcut.

A cleaner looking alternative is this library which seems to provide the same functionality. I still have to test it.

Both these solutions should work with Microsoft's NotificationsExtensions librabry, which is a precursor of the UWP Toolkit and helps with generating the XML code Toasts are made of.