编辑
2025-10-13
C#
00

目录

环境准备
NuGet 包安装
必要的 using 语句
基础实现
通知类型
基本文本通知
带图片的通知
带按钮的交互式通知
带进度条的通知
计划通知
事件处理

Windows 通知是与用户进行交互的重要方式。通过 Microsoft.Toolkit.Uwp.Notifications,我们可以轻松实现丰富的通知功能,包括基本通知、图片通知、交互式通知等。

环境准备

NuGet 包安装

PowerShell
Install-Package Microsoft.Toolkit.Uwp.Notifications

必要的 using 语句

C#
using Microsoft.Toolkit.Uwp.Notifications; using Windows.UI.Notifications; using Windows.Data.Xml.Dom;

项目中对版本要求,一定是Windows10以上版本。

image.png

基础实现

首先,我们创建一个通知辅助类来封装常用的通知功能:

C#
public class NotificationHelper { /// <summary> /// 发送基本通知 /// </summary> /// <param name="title">通知标题</param> /// <param name="content">通知内容</param> public static void SendBasicNotification(string title, string content) { var builder = new ToastContentBuilder() .AddText(title) .AddText(content); builder.Show(); } }

通知类型

基本文本通知

C#
public static void SendBasicNotification(string title, string content) { var builder = new ToastContentBuilder() .AddText(title) .AddText(content); builder.Show(); }

image.png

带图片的通知

C#
public static void SendNotificationWithImage(string title, string content, string imageUrl) { var builder = new ToastContentBuilder() .AddText(title) .AddText(content) .AddInlineImage(new Uri(imageUrl)); builder.Show(); }

这里图片可以是本地的,需要全路径

image.png

带按钮的交互式通知

C#
public static void SendNotificationWithActions(string title, string content) { var builder = new ToastContentBuilder() .AddText(title) .AddText(content) .AddButton(new ToastButton() .SetContent("确定") .AddArgument("action", "confirm")) .AddButton(new ToastButton() .SetContent("取消") .AddArgument("action", "cancel")); builder.Show(); }

image.png

带进度条的通知

C#
/// <summary> /// 发送带进度条和状态文本的通知 /// </summary> /// <param name="title">标题</param> /// <param name="content">内容</param> /// <param name="progressValue">进度值(0到1之间)</param> /// <param name="status">进度状态文本</param> public static void SendProgressNotificationWithStatus(string title, string content , double progressValue, string status) { // 确保进度值在0到1之间 progressValue = Math.Max(0, Math.Min(1, progressValue)); var builder = new ToastContentBuilder() .AddText(title) .AddText(content) .AddProgressBar("progressBar1", progressValue, valueStringOverride: $"{(progressValue * 100):F0}%", status: status); builder.Show(); }

image.png

计划通知

C#
/// <summary> /// 发送计划通知 /// </summary> /// <param name="title">通知标题</param> /// <param name="content">通知内容</param> /// <param name="scheduleTime">计划时间</param> public static void SendScheduledNotification(string title, string content, DateTimeOffset scheduleTime) { try { // 验证时间 if (scheduleTime <= DateTimeOffset.Now) { throw new ArgumentException("计划时间必须是将来的时间"); } // 创建通知内容 var builder = new ToastContentBuilder() .AddText(title) .AddText(content); // 创建计划通知 var scheduledToast = new ScheduledToastNotification( builder.GetXml(), scheduleTime); // 注册应用程序 ToastNotificationManagerCompat.CreateToastNotifier(); // 发送通知 ToastNotificationManagerCompat.CreateToastNotifier().AddToSchedule(scheduledToast); } catch (Exception ex) { Console.WriteLine($"发送计划通知失败: {ex.Message}"); throw; } }
C#
internal class Program { static void Main(string[] args) { try { // 注册COM组件 ToastNotificationManagerCompat.OnActivated += toastArgs => { // 处理通知激活事件 Console.WriteLine("通知被激活"); }; // 发送一个计划通知 NotificationHelper.SendScheduledNotification( "测试通知", "这是一条计划通知", DateTimeOffset.Now.AddMinutes(1)); Console.WriteLine("通知已计划发送"); Console.WriteLine("按任意键退出..."); Console.ReadKey(); } catch (Exception ex) { Console.WriteLine($"发生错误: {ex.Message}"); Console.ReadKey(); } } }

image.png

事件处理

C#
internal class Program { static void Main(string[] args) { // 初始化通知 ToastNotificationManagerCompat.OnActivated += ToastNotificationManagerCompat_OnActivated; // 发送通知 SendNotificationWithActions("测试标题", "测试内容"); // 保持程序运行以等待通知响应 Console.WriteLine("程序运行中,等待通知响应..."); Console.WriteLine("按 Enter 键退出"); Console.ReadLine(); } private static void ToastNotificationManagerCompat_OnActivated(ToastNotificationActivatedEventArgsCompat e) { // 解析参数 var args = ToastArguments.Parse(e.Argument); // 获取 action 参数 string action = args["action"]; // 根据不同的按钮处理不同的逻辑 switch (action) { case "confirm": Console.WriteLine("确定按钮被点击"); // 在这里添加确定按钮的处理逻辑 break; case "cancel": Console.WriteLine("取消按钮被点击"); // 在这里添加取消按钮的处理逻辑 break; } } public static void SendNotificationWithActions(string title, string content) { var builder = new ToastContentBuilder() .AddText(title) .AddText(content) .AddButton(new ToastButton() .SetContent("确定") .AddArgument("action", "confirm")) .AddButton(new ToastButton() .SetContent("取消") .AddArgument("action", "cancel")); builder.Show(); } }

image.png

以上就是使用 Microsoft.Toolkit.Uwp.Notifications 实现 Windows 通知的完整指南。通过这些代码和说明,您可以在应用程序中实现丰富的通知功能。根据实际需求,您可以选择合适的通知类型,并进行相应的定制化开发。

本文作者:技术老小子

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!