Dino Windows 8 学习笔记(十七)-- 定时提醒

Dino Windows 8 学习笔记(十七)-- 定时提醒
刚开始的思路是使用Background Task,寄希望于TimeTrigger中可以设置具体的时间,这样,当时间到了的时候执行后台人务,Toast出现。但是TimeTrigger主要是用于LockScreen App,而且时间只能设置成每15分钟一次,其他时间不行。TimeTrigger的主要任务是每15分钟更新一次LockSreen App的Badge。
      既然TimeTrigger不能用,那么就得另寻他法。在Notification命名空间下有这么一个类:ScheduledToastNotification,它的构造函数是这样的:


public:
ScheduledToastNotification(
  XmlDocument^ content, 
  DateTime deliveryTime
)

该构造函数构造一个定时的Toast消息,并且只显示一次。
有了该方法,定时推送就比较简单了,只要在你的代码中实现以下代码:

    Windows::Globalization::Calendar^ cal =  ref  new Windows::Globalization::Calendar();
    cal->AddMinutes(1);
    Windows::Foundation::DateTime dateToFormat = cal->GetDateTime();
    

    ToastTemplateType toastTemplate = ToastTemplateType::ToastText01;
    XmlDocument^ toastXml = ToastNotificationManager::GetTemplateContent(toastTemplate);
    XmlNodeList^ toastTextElements = toastXml->GetElementsByTagName("text");
    toastTextElements->Item(0)->InnerText = "You need to do a work!";

    
    auto notification =  ref  new ScheduledToastNotification(toastXml, dateToFormat);
    ToastNotificationManager::CreateToastNotifier()->AddToSchedule(notification);

这里DateTime是个结构体类型,我们只能通过Calender对象来获取时间,因为我们最后建立的是一个ScheduledToastNotification类型的Toast,所以最后要哦那个AddToShedule来显示出来。

你可能感兴趣的:(Dino Windows 8 学习笔记(十七)-- 定时提醒)