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

4

WinUI 3是否具有添加桌面通知的功能?

请参阅参考文献(见下文)

桌面通知


你有看过这个吗:https://github.com/CommunityToolkit/WindowsCommunityToolkit 和他们的 toast 通知吗? - Rand Random
谢谢,我会调查一下,如果有任何问题我会与您联系。 - briannarich
2个回答

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;
}

更新于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上找到。


1
  1. 安装Nuget包:
    • Microsoft.Toolkit.Uwp.Notifications
  2. 使用'ToastContentBuilder'构建通知内容。
  3. 处理激活
    • 在显示通知后,您可能需要处理用户单击通知。无论是在用户单击后打开特定内容,打开您的应用程序,还是在用户单击通知时执行操作。

参考资料:

Microsoft Docs


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接