2017-11-25 49 views
0

我想创建定期托管通知,但我找到了一个不正确的解决方案,因为snoozeInterva只是一个“延期”。定期预约toastnotification UWP

代码:

public sealed partial class MainPage : Page 
{ 
    const string TOAST = @" 
         <toast> 
          <visual> 
          <binding template=""ToastTest""> 
           <text>Hello Toast</text> 
          </binding> 
          </visual> 
          <audio src =""ms-winsoundevent:Notification.Mail"" loop=""true""/> 
         </toast>"; 

    public MainPage() 
    { 
     this.InitializeComponent(); 
    } 

    private void btnNotification_Click(object sender, RoutedEventArgs e) 
    { 
     var when = DateTime.Now.AddSeconds(6); 
     var offset = new DateTimeOffset(when); 

     Windows.Data.Xml.Dom.XmlDocument xml = new Windows.Data.Xml.Dom.XmlDocument(); 
     xml.LoadXml(TOAST); 
     ScheduledToastNotification toast = new ScheduledToastNotification(xml, offset, TimeSpan.FromSeconds(60), 5); 
     toast.Id = "IdTostone"; 
     toast.Tag = "NotificationOne"; 
     ToastNotificationManager.CreateToastNotifier().AddToSchedule(toast); 
    } 
} 

的话..我怎么能在13:00创建定期Toast通知,例如每天?

在此先感谢!

回答

1

不幸的是,没有方便的方法来做到这一点。如果您需要每天同时发送祝词,您可以安排一堆敬酒通知,比如说,预先一个月,每天一个。然后,如果您需要更改所有敬酒的时间,请使用ToastNotificationManager课程将其全部从敬酒时间表中删除,并在适当的时间创建新的预定敬酒。

事情是这样的:

private void ScheduleToast(DateTime scheduledTime) 
    { 
     const string TOAST = @" 
        <toast> 
         <visual> 
         <binding template=""ToastTest""> 
          <text>Hello Toast</text> 
         </binding> 
         </visual> 
         <audio src =""ms-winsoundevent:Notification.Mail"" loop=""true""/> 
        </toast>"; 

     Windows.Data.Xml.Dom.XmlDocument xml = new Windows.Data.Xml.Dom.XmlDocument(); 
     xml.LoadXml(TOAST); 

     ScheduledToastNotification toast = new ScheduledToastNotification(xml, scheduledTime); 
     toast.Id = "IdTostone" + scheduledTime.ToString(); 
     toast.Tag = "NotificationOne"; 
     toast.Group = "MyEverydayToasts"; 
     ToastNotificationManager.CreateToastNotifier().AddToSchedule(toast); 
    } 

    private void RescheduleToastsForTheNextDays(TimeSpan timeOfDay, int nDays = 30) 
    { 
     ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier(); 
     IReadOnlyList<ScheduledToastNotification> scheduledToasts = toastNotifier.GetScheduledToastNotifications(); 
     foreach(ScheduledToastNotification toast in scheduledToasts) 
      toastNotifier.RemoveFromSchedule(toast); 

     for (int i=0; i<nDays; i++) 
     { 
      DateTime scheduledTime = DateTime.Today + timeOfDay + TimeSpan.FromDays(i); 

      if (scheduledTime > DateTime.Now) 
       ScheduleToast(scheduledTime); 
     } 
    } 
+0

我想问即使有点平庸...你喜欢Windows的应用程序时,重复平日报警的结构这样一个问题?我请他澄清这些想法。感谢您的帮助.. – LightGreen

+0

@LightGreen是的,我认为这很好。也许不完美。 –

+0

再次感谢您的帮助.. – LightGreen