使用内置的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;
}
Run Code Online (Sandbox Code Playgroud)
更新2023-03-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;
}
Run Code Online (Sandbox Code Playgroud)
Microsoft Learn提供了更高级的示例。