WinUI 3是否具有添加桌面通知的功能?
请参阅参考文献(见下文)
使用内置的AppNotification类:
// using Microsoft.Windows.AppNotifications;
public static bool SendNotificationToast(string title, string message)
{
var xmlPayload = new string($@"
<toast>
<visual>
<binding template=""ToastGeneric"">
<text>{title}</text>
<text>{message}</text>
</binding>
</visual>
</toast>");
var toast = new AppNotification(xmlPayload);
AppNotificationManager.Default.Show(toast);
return toast.Id != 0;
}
更新于2023年3月12日
自从Windows App SDK 1.2版本以后,您可以使用AppNotificationBuilder类。
public static bool SendNotificationToast(string title, string message)
{
var toast = new AppNotificationBuilder()
.AddText(title)
.AddText(message)
.BuildNotification();
AppNotificationManager.Default.Show(toast);
return toast.Id != 0;
}
更高级的示例可在Microsoft Learn上找到。
参考资料: