如何使用 WinUI 3 添加桌面通知?

bri*_*ich 4 .net c# xaml winui winui-3

WinUI 3有添加桌面通知的功能吗?

请参阅参考资料(见下文)

桌面通知

Kub*_*tak 7

使用内置的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提供了更高级的示例。