以前看到Windows 10的气泡通知觉得很有意思,但是一直不知道该如何实现。最近一次上网冲浪过程中偶然的机会看到了相关资料就自己来试试。本文介绍了在WPF框架下发送Win10 Toast通知的方法。
代码见Github仓库:

m7XQ_rvDolg0qX20FU7dVmQhHDcu9ueVN37dMdlAv4g.png

简单Toast通知的使用

简单的Toast通知

new ToastContentBuilder()
    .AddArgument("action", "viewConversation")
    .AddArgument("conversationId", 9813)
    .AddText("Andrew sent you a picture")
    .AddText("Check this out, The Enchantments in Washington!")
    .Show();

vOVhBaTWHOoo7s8EK6btz1srofpwNFgcBj01dsf3Ebc.png

带按钮的Toast通知

var builder = new ToastContentBuilder()
    .AddArgument("action", "viewConversation")
    .AddArgument("conversationId", 9813)

    .AddText("Some text")

    .AddButton(new ToastButton()
        .SetContent("Archive")
        .AddArgument("action", "archive"))
    .AddButton(new ToastButton()
        .SetContent("Show")
        .AddArgument("action", "archive"))
    ;
builder.Show();

PoI-OtzjirVaihlScpCLnXiYCnGnUaQbXyzsgcCt9l0.png

工程和Windows SDK的设置

如果看不到Show 方法,则可以注意文档中的这句话

Not seeing the Show() method? Make sure you have version 7.0, and if you're using .NET 6 (or later), then your TFM must be net6.0-windows10.0.17763.0 or greater

要使用Toast的API 需要.NET 6 以上,并设置目标操作系统为Windows,版本至少需要10.0.17763.0

eIzojWKaHea_Sb4tK2sXKpLcsHb2fJDfPFvPNxdujrY.png

参考链接

https://learn.microsoft.com/zh-cn/windows/apps/design/shell/tiles-and-notifications/send-local-toast?tabs=desktop

https://learn.microsoft.com/zh-cn/windows/apps/design/shell/tiles-and-notifications/adaptive-interactive-toasts?tabs=toolkit