9.1 Toast通知
Toast通知是在屏幕最頂上彈出來的臨時通知,是Windows Phone通用的彈出式短暫的通知,默認的系統消息都是采用Toast通知的形式,比如當你手機收到短信的時候,在手機的頂端彈出的消息就是Toast通知,點擊該通知你可以直接進入短信的詳情頁面,通知顯示的時間是7秒鍾,7秒鍾后會自動消失,如果你想快速關閉通知,可以采用在Toast通知上面向右滑動的手勢便可以快速地關閉掉當前的Toast通知。除了系統使用這樣的Toast通知之外,第三方的應用程序也是可以使用這種通知的形式,Toast通知不僅僅可以在打開應用程序的時候彈出,也可以在應用程序關閉的情況進行定時通知或者推送通知來進行發送,這也是Toast通知的最大的魅力所在。Toast通知只應該用於用戶特別感興趣的信息,通常涉及某種形式的用戶選擇。因此,收到IM聊天請求和用戶選擇接收的信息都是不錯的選擇。但是,當你考慮使用Toast通知時,你必須認識到非常重要的一點,由於它的短暫性或由於用戶設置,用戶可能錯過而未看到它。Toast通知專為與鎖屏提醒、磁貼通知及應用中UI結合使用而設計,旨在讓用戶即時了解你應用中的相關事件或項目。Toast通知的實現還會分為兩種形式,一種是在應用程序本地實現,另外一種是在雲端實現,進行推送,那么我們這一小節主要是講解在應用程序本地實現的Toast通知,在雲端實現的Toast通知,可以參考第12章推送通知的內容講解。
9.1.1 創建一個通知消息
你的應用要想通過Toast通知通信,必須在應用的清單文件Package.appxmanifest中聲明它支持 Toast,否調用Toast通知相關的API將不會生效。在Package.appxmanifest的可視化界面中,找到“Application”->“Notifications”->“Toast capable”,然后設置為“Yes”。打開Package.appxmanifest的代碼視圖文件,可以看到m3:VisualElements元素的ToastCapable屬性設置為true,代碼如下所示:
<Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="ToastDemo.App">
<m3:VisualElements …… ToastCapable="true">
……
</m3:VisualElements>
</Application>
添加了Toast通知的權限之后,我們來看一段創建Toast通知並彈出的代碼示例:
// 獲取Tosat通知的模板
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
// 找到模板中“'text'”元素,然后添加通知的內容
XmlNodeList elements = toastXml.GetElementsByTagName("text");
elements[0].AppendChild(toastXml.CreateTextNode("A sample toast"));
// 通過通知的模板創建一個Toast通知
ToastNotification toast = new ToastNotification(toastXml);
// 彈出通知
ToastNotificationManager.CreateToastNotifier().Show(toast);
下面我們再根據上面的代碼來一步步地都講解Toast通知的編程步驟:
(1)Toast通知的模板
每個Toast通知的格式都會對應着一個XML的模板,在我們創建一個Toast通知對象之前,我們首先需要選擇Toast通知的模板。Toast通知的模板由ToastTemplateType來進行描述,可以通過Toast通知管理類ToastNotificationManager類的靜態方法GetTemplateContent來獲取對應的Toast通知模板。在Windows Phone的應用程序里面主要會用到ToastImageAndText01和ToastImageAndText02這兩種類型的模板。
1)ToastText01模板表示是一個最簡單的Toast通知模板,只有通知的內容信息,它的XML格式如下所示:
<toast>
<visual>
<binding template="ToastText01">
<text id="1">bodyText</text>
</binding>
</visual>
</toast>
2)ToastText02模板表示是包含消息頭和消息體的模板,消息頭是一個加粗文本字符串,消息頭和消息體會使用空格隔開,它的XML格式如下所示:
<toast>
<visual>
<binding template="ToastText02">
<text id="1">headlineText</text>
<text id="2">bodyText</text>
</binding>
</visual>
</toast>
(2)添加Toast通知的內容
獲取了Toast通知的模板對象之后,我們可以通過XML對象XmlDocument對象的相關屬性和方法來修改XML的內容,從而實現在Toast通知的XML模板上添加消息的內容信息。
(3)創建Toast通知的對象
添加好Toast通知的內容之后,我們就可以使用XmlDocument對象來初始化一個Toast通知對象,這時候可以使用ToastNotification類的構造方法ToastNotification(XmlDocument content)方法來進行初始化,這也是Toast通知唯一的構造方法。
(4)彈出Toast通知
彈出Toast通知可以使用ToastNotifier類的Show方法,ToastNotifier類是表示Toast通知的通知操作管理器,使用該類可以實現獲取Toast列表,打開Toast通知,取下Toast通知等操作。
9.1.2 定期 Toast 通知
Toast通知不僅僅可以在應用程序運行的時候彈出,還可以在應用程序離開前台的時候彈出,這時候可以使用定期Toast來實現。定期Toast通知就是通過預設未來的一個時間,在這個時間點上彈出Toast通知,如果應用程序這時候不在前台運行,Toast通知也可以運行,用戶點擊Toast通知的時候可以直接進入當前的應用程序。
ScheduledToastNotification類表示是定期Toast通知的信息類,你可以使用構造方法ScheduledToastNotification(XmlDocument content, DateTimeOffset deliveryTime)方法來創建一個ScheduledToastNotification對象,然后添加到Toast通知的定時計划里面,其中content參數表示是消息的XML內容,deliveryTime表示是消息彈出的時間。示例代碼如下所示:
// 創建一個ToastText02的消息模板
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
// 獲取XML模板的text元素
XmlNodeList toastNodeList = toastXml.GetElementsByTagName("text");
// 設置通知頭信息
toastNodeList.Item(0).AppendChild(toastXml.CreateTextNode("Toast title"));
// 設置通知體信息
toastNodeList.Item(1).AppendChild(toastXml.CreateTextNode("Toast content"));
// 獲取一個距離現在還有10秒鍾的時間點
DateTime startTime = DateTime.Now.AddSeconds(10);
// 使用XML模板和通知的時間創建一個ScheduledToastNotification對象
ScheduledToastNotification recurringToast = new ScheduledToastNotification(toastXml, startTime);
// 設置通知的ID
recurringToast.Id = "ScheduledToast1";
// 把定時Toast通知添加到通知計划里面
ToastNotificationManager.CreateToastNotifier().AddToSchedule(recurringToast);
9.1.3 實例演示:Toast通知
下面給Toast通知的示例:實現ToastText01和ToastText02兩種模板以及定時通知。
代碼清單9-1:Toast通知(第9章\Examples_9_1)
MainPage.xaml文件主要代碼 ------------------------------------------------------------------------------------------------------------------ <StackPanel> <Button Content="ToastText01模板通知" x:Name="toastText01" Click="toastText01_Click" Width="370"></Button> <Button Content="ToastText02模板通知" x:Name="toastText02" Click="toastText02_Click" Width="370"></Button> <Button Content="XML模板通知" x:Name="toastXML" Click="toastXML_Click" Width="370"></Button> <Button Content="定時通知" x:Name="scheduledToast" Click="scheduledToast_Click" Width="370"></Button> <TextBlock x:Name="info"></TextBlock> </StackPanel>
MainPage.xaml.cs文件主要代碼 ------------------------------------------------------------------------------------------------------------------ // 彈出ToastText01模板的Toast通知 private void toastText01_Click(object sender, RoutedEventArgs e) { XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01); XmlNodeList elements = toastXml.GetElementsByTagName("text"); elements[0].AppendChild(toastXml.CreateTextNode("Hello Windows Phone 8.1")); ToastNotification toast = new ToastNotification(toastXml); toast.Activated += toast_Activated; toast.Dismissed += toast_Dismissed; toast.Failed += toast_Failed; ToastNotificationManager.CreateToastNotifier().Show(toast); } // 彈出ToastText02模板的Toast通知 private void toastText02_Click(object sender, RoutedEventArgs e) { XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02); XmlNodeList elements = toastXml.GetElementsByTagName("text"); elements[0].AppendChild(toastXml.CreateTextNode("WP8.1")); elements[1].AppendChild(toastXml.CreateTextNode("Hello Windows Phone 8.1")); ToastNotification toast = new ToastNotification(toastXml); toast.Activated += toast_Activated; toast.Dismissed += toast_Dismissed; toast.Failed += toast_Failed; ToastNotificationManager.CreateToastNotifier().Show(toast); } // 直接使用XML字符串來拼接出ToastText02模板的Toast通知 private void toastXML_Click(object sender, RoutedEventArgs e) { string toastXmlString = "<toast>" + "<visual>" + "<binding template='ToastText02'>" + "<text id='1'>WP8.1</text>" + "<text id='2'>" + "Received: " + DateTime.Now.ToLocalTime() + "</text>" + "</binding>" + "</visual>" + "</toast>"; XmlDocument toastXml = new XmlDocument(); toastXml.LoadXml(toastXmlString); ToastNotification toast = new ToastNotification(toastXml); toast.Activated += toast_Activated; toast.Dismissed += toast_Dismissed; toast.Failed += toast_Failed; ToastNotificationManager.CreateToastNotifier().Show(toast); } // Toast通知彈出失敗的事件 async void toast_Failed(ToastNotification sender, ToastFailedEventArgs args) { await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { info.Text = "Toast通知失敗:" + args.ErrorCode.Message; }); } // Toast通知消失的事件,當通知自動消失或者手動取消會觸發該事件 async void toast_Dismissed(ToastNotification sender, ToastDismissedEventArgs args) { await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { info.Text = "Toast通知消失:" + args.Reason.ToString(); }); } // Toast通知激活的事件,當通知彈出時,點擊通知會觸發該事件 async void toast_Activated(ToastNotification sender, object args) { await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { info.Text = "Toast通知激活"; }); } // 定時Toast通知 private void scheduledToast_Click(object sender, RoutedEventArgs e) { XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02); XmlNodeList toastNodeList = toastXml.GetElementsByTagName("text"); toastNodeList.Item(0).AppendChild(toastXml.CreateTextNode("Toast title")); toastNodeList.Item(1).AppendChild(toastXml.CreateTextNode("Toast content")); DateTime startTime = DateTime.Now.AddSeconds(3); ScheduledToastNotification recurringToast = new ScheduledToastNotification(toastXml, startTime); recurringToast.Id = "ScheduledToast1"; ToastNotificationManager.CreateToastNotifier().AddToSchedule(recurringToast); }
本文來源於《深入淺出Windows Phone 8.1 應用開發》
WP8.1 Runtime文章列表:http://www.cnblogs.com/linzheng/p/3998037.html
源代碼下載:http://vdisk.weibo.com/s/zt_pyrfNHb99O
歡迎關注我的微博@WP林政 微信公眾號:wp開發(號:wpkaifa)
WP8.1技術交流群:372552293