剛開始的思路是使用Background Task,寄希望于TimeTrigger中可以設(shè)置具體的時間,這樣,當時間到了的時候執(zhí)行后臺人務(wù),Toast出現(xiàn)。但是TimeTrigger主要是用于LockScreen App,而且時間只能設(shè)置成每15分鐘一次,其他時間不行。TimeTrigger的主要任務(wù)是每15分鐘更新一次LockSreen App的Badge。
既然TimeTrigger不能用,那么就得另尋他法。在Notification命名空間下有這么一個類:ScheduledToastNotification,它的構(gòu)造函數(shù)是這樣的:
public:
ScheduledToastNotification(
XmlDocument^ content,
DateTime deliveryTime
)
該構(gòu)造函數(shù)構(gòu)造一個定時的Toast消息,并且只顯示一次。
有了該方法,定時推送就比較簡單了,只要在你的代碼中實現(xiàn)以下代碼:
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是個結(jié)構(gòu)體類型,我們只能通過Calender對象來獲取時間,因為我們最后建立的是一個ScheduledToastNotification類型的Toast,所以最后要哦那個AddToShedule來顯示出來。
posted on 2013-02-01 10:30
Dino-Tech 閱讀(502)
評論(0) 編輯 收藏 引用