c# - 如何在 WPF 应用程序中集成 toast 通知?
问题描述
我想向我的 wpf 应用程序添加 toast 通知。
我已按照Microsoft的从桌面 C# 应用程序发送本地 toast 通知,但我被困在第 5 步。
我不确定如何使这段代码工作:
// 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);
另外,我在 .csproj 中添加了对,<TargetPlatformVersion>10.0</TargetPlatformVersion>
的引用。Windows.Data
Windows.UI
此时,我遇到了 2 个错误:
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'
当我添加Windows.Foundation.UniversalApiContract
作为参考时,C:\Program Files (x86)\Windows Kits\10\References\10.0.18362.0\Windows.Foundation.UniversalApiContract\8.0.0.0\Windows.Foundation.UniversalApiContract.winmd
我收到以下错误:
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'
我该如何解决?
请注意,我也尝试使用“现成示例”,但结果相同。
解决方案
新的:
有一种在 WPF 应用程序中使用 UWP Toast 通知的方法。
- 添加
<TargetPlatformVersion>10.0</TargetPlatformVersion>
到 .csproj - 如果您安装了任何软件包,请单击
packages.config
文件并选择Migrate packages.config to PackageReference...
(重要步骤 - 这是我缺少的东西) - 现在在包管理器控制台中安装 UWP.Notifications 包,例如:
Install-Package Microsoft.Toolkit.Uwp.Notifications -Version 7.0.2
检查版本
现在,您应该可以使用所有 uwp 通知功能了。
老的:
正如@Andy 在评论中建议的那样,有现成的 NuGet 包实现:github.com/Federerer/Notifications.Wpf
如果您只想要一个没有 onClick 事件等的简单通知,您可以使用此解决方案:
- 添加
<TargetPlatformVersion>10.0</TargetPlatformVersion>
到 .csproj - 添加对
Windows.UI
和的引用Windows.Data
- 添加以下用法:
using Windows.Data.Xml.Dom; using Windows.UI.Notifications;
- 使用此代码显示通知:
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);
推荐阅读
- jenkins - 退出代码以设置构建不稳定 - Jenkins DSL 脚本
- ansible - 如何仅获取 ansible with_items 的 item.item 中的特定元素
- rust - 创建盒装特征背后的机制如何运作?
- android - 如何制作像imagebutton这样的bottomnavigationview项目
- php - 在heredoc中使用增量运算符
- powerapps - 在 Power Apps 中应用搜索和过滤
- ios - 如何停止动画并立即开始新动画?
- python - 有没有办法在另一个 Python 模块中调用函数而不明确说明函数名?
- r - 如何计算两种状态之间转换的频率?
- python - 如何发送自定义帮助 PM