我正在尝试使用我的WPF C# 桌面应用程序显示 Windows 10 Toasts。
遗憾的是,在非 UWP 或应用商店应用程序中,有关 Windows 10 通知的 API 和一般支持似乎非常有限且困惑。 最近 UWP Community Toolkit已出版,这似乎试图让我们的事情变得更容易。 还有这个商店应用程序,Notifications Visualizer ,这有助于制作这样的 toast :
我继续尝试使用 C# 和 UWP Community Toolkit 提供的 API 生成 Toast。
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
不幸的是,无论我尝试什么,我似乎都无法得到相同的结果,由于某种原因图像总是丢失:
我发现的有关这些通知的大部分信息要么已过时,要么毫无用处。 有人可以对此有所了解吗? 谢谢。
最佳答案
没有办法单独使用 UWP 工具包
看起来使用 .NET Standard 这只能通过两个步骤实现,其中之一超出了 UWP 工具包的范围。
.NET 标准应用程序需要 COM 服务器和特殊开始菜单快捷方式才能正确使用 Windows 10 操作中心。 UWP Apps 似乎不需要或已经具备等效功能。 这两个步骤应该在应用程序安装期间执行,而这显然是 Microsoft UWP Toolkit 没有参与的事情。 因此,仅靠 UWP 工具包不仅不能而且永远无法提供完整的解决方案,以自包含的方式显示适用于 .NET Standard 的 Windows 10 Toast。
备选方案
我发现了一个不起眼的 C# Project on Github名称中包含“Microsoft”,无需 UWP 工具包即可开箱即用。 它要求您提供一个 GUID 和一个 AppID 字符串,然后用于注册 COM 服务器和创建快捷方式。
一个看起来更干净的替代品是 this library这似乎提供了相同的功能。我还得测试一下。
这两种解决方案都应该适用于 Microsoft 的 NotificationsExtensions库,它是 UWP 工具包的前身,有助于生成构成 Toast 的 XML 代码。
关于c# - WPF 桌面应用程序,Windows 10 Notification Toast 2016(UWP 社区工具包),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39744331/