UWP中如何调度Toast通知并为其设置Tag标签?
解决方案
方法1:使用Schedule方法的委托重载
多数版本的Microsoft.Toolkit.Uwp.Notifications包中,Schedule方法提供了第二个参数的重载,支持传入委托直接配置生成的计划通知实例,和你使用Show方法的写法逻辑完全一致:
new ToastContentBuilder() .AddText("Hello world!") .SetToastDuration(ToastDuration.Long) .Schedule(DateTime.Now.AddSeconds(15), scheduledToast => { scheduledToast.Tag = "540"; // 可同时配置Group、ExpirationTime等其他计划通知属性 });
方法2:手动构建计划通知实例注册
如果你使用的工具包版本没有提供上述重载,可以手动生成ScheduledToastNotification对象配置属性后,注册到系统通知调度队列:
// 构建通知内容 var toastContent = new ToastContentBuilder() .AddText("Hello world!") .SetToastDuration(ToastDuration.Long) .GetToastContent(); // 初始化计划通知对象并配置Tag var scheduledNotification = new ScheduledToastNotification( content: toastContent.GetXml(), deliveryTime: DateTime.Now.AddSeconds(15)) { Tag = "540" }; // 注册到调度队列 ToastNotificationManager.CreateToastNotifier().AddToSchedule(scheduledNotification);
两种方案都可以实现调度延迟通知的同时设置Tag属性,后续你也可以通过Tag和Group组合来管理、移除已调度未触发的通知。
内容的提问来源于stack exchange,提问作者Xavier
相关产品推荐
相关产品推荐

