问题
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:
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:
Most information I found concerning these notifications are either outdated or useless. Can someone please shed some light on this? Thank you.
回答1:
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.
来源:https://stackoverflow.com/questions/39744331/wpf-desktop-app-windows-10-notification-toast-2016-uwp-community-toolkit