Windows 通知是与用户进行交互的重要方式。通过 Microsoft.Toolkit.Uwp.Notifications,我们可以轻松实现丰富的通知功能,包括基本通知、图片通知、交互式通知等。
PowerShellInstall-Package Microsoft.Toolkit.Uwp.Notifications
C#using Microsoft.Toolkit.Uwp.Notifications;
using Windows.UI.Notifications;
using Windows.Data.Xml.Dom;
项目中对版本要求,一定是Windows10以上版本。
首先,我们创建一个通知辅助类来封装常用的通知功能:
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();
}
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();
}
这里图片可以是本地的,需要全路径
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();
}
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();
}
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();
}
}
}
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();
}
}
以上就是使用 Microsoft.Toolkit.Uwp.Notifications 实现 Windows 通知的完整指南。通过这些代码和说明,您可以在应用程序中实现丰富的通知功能。根据实际需求,您可以选择合适的通知类型,并进行相应的定制化开发。
本文作者:技术老小子
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!