介紹
重新想象 Windows 8 Store Apps 之 通知
Toast - 通知的應用
Tile - 瓷貼的應用
Badge - 徽章的應用
Badge - 輪詢服務端以更新 Badge 通知
樣本
1、示範 toast 的基 本應用
Notification/Toast/Demo.xaml
<Page x:Class="XamlDemo.Notification.Toast.Demo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Notification.Toast" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <StackPanel Margin="120 0 0 0"> <TextBox Name="lblMsg" Height="100" TextWrapping="Wrap" AcceptsReturn="True" FontSize="14.667" Margin="0 0 10 0" /> <!--彈出通知(通知顯示的時間較短)--> <Button Name="btnShortToast" Content="Short Toast" Click="btnShortToast_Click_1" Margin="0 10 0 0" /> <!--彈出通知(通知顯示的時間較長)--> <Button Name="btnLongToast" Content="Long Toast" Click="btnLongToast_Click_1" Margin="0 10 0 0" /> <TextBlock Name="lblStatus" FontSize="14.667" Margin="0 10 0 0" /> </StackPanel></Page>
Notification/Toast/Demo.xaml.cs
/* * Toast - 通知 * * ToastNotification - Toast 通知 * Content - Toast 的內容,XmlDocument 類型的資料,唯讀,其需要在建構函式中指定 * ExpirationTime - Toast 通知的到期時間,即如果系統在此屬性指定的時間到了之後還沒有顯示對應的 Toast 通知,那麼之後也不要再顯示了 * Activated - 使用者點擊通知時觸發的事件 * Dismissed - 通知消失時觸發的事件(事件參數 ToastDismissedEventArgs) * Failed - 通知顯示失敗時觸發的事件 * * ToastDismissedEventArgs - Toast 消失時的事件參數 * Reason - 通知消失的原因(Windows.UI.Notifications.ToastDismissalReason 枚舉) * UserCanceled - 使用者關閉通知 * ApplicationHidden - 通過 ToastNotifier.Hide() 關閉通知 * TimedOut - 自動關閉通知(短通知 7 秒,長通知 25 秒) * * ToastNotificationManager - Toast 通知管理器 * CreateToastNotifier() - 建立一個 Toast 通知器,返回 ToastNotifier 類型的資料 * CreateToastNotifier(string applicationId) - 為指定的 app 建立一個 Toast 通知器(這裡指定的是同一 package 內的其他 app 。註:一個 package 中可以有多個 app,但是目前無法通過商店審核) * * ToastNotifier - Toast 通知器 * Show() - 顯示指定的 ToastNotification * Hide() - 隱藏指定的 ToastNotification * Setting - 擷取通知設定(Windows.UI.Notifications.NotificationSetting 枚舉) * Enabled - 通知可被顯示 * DisabledForApplication - 使用者禁用了此應用程式的通知 * DisabledForUser - 使用者禁用了此電腦此賬戶的所有通知 * DisabledByGroupPolicy - 管理員通過組策略禁止了此電腦上的所有通知 * DisabledByManifest - 應用程式未在 Package.appxmanifest 中啟用 Toast 通知(對應“應用程式 UI”中的小徽標) * * * 註: * 目前系統支援 8 套 Toast 模板:詳見:ToastWithText.xaml 和 ToastWithImageText.xaml * Toast 通知右下角的應用程式徽標,採用的是 Package.appxmanifest 中配置的小徽標,即 30*30 像素的徽標 * 不能在模擬器中運行 */ using NotificationsExtensions.ToastContent;using System;using Windows.Data.Xml.Dom;using Windows.UI.Core;using Windows.UI.Notifications;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls; namespace XamlDemo.Notification.Toast{ public sealed partial class Demo : Page { public Demo() { this.InitializeComponent(); } // 彈出短時通知 private void btnShortToast_Click_1(object sender, RoutedEventArgs e) { // 用一個開源的 Toast 構造器來建立 ToastNotification,具體實現參見:NotificationsExtensions/ToastContent.cs IToastText01 templateContent = ToastContentFactory.CreateToastText01(); templateContent.TextBodyWrap.Text = "我是通知本文,可換行,最多三行。我是通知本文,可換行,最多三行。我是通知本文,可換行,最多三行。"; templateContent.Duration = ToastDuration.Short; // 短時通知,預設值 IToastNotificationContent toastContent = templateContent; ToastNotification toast = toastContent.CreateNotification(); // 監聽 ToastNotification 的相關事件 toast.Activated += toast_Activated; toast.Failed += toast_Failed; toast.Dismissed += toast_Dismissed; // 彈出指定的通知 ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier(); toastNotifier.Show(toast); lblStatus.Text += "NotificationSetting: " + toastNotifier.Setting.ToString(); lblStatus.Text += Environment.NewLine; // 顯示此 toast 的 xml lblMsg.Text = toastContent.GetContent(); } // 彈出長時通知 private void btnLongToast_Click_1(object sender, RoutedEventArgs e) { // 手工構造 Toast 通知的 xml var toastXmlString = "<toast duration='long'>" + "<visual version='1'>" + "<binding template='ToastText01'>" + "<text id='1'>我是通知本文,可換行,最多三行。我是通知本文,可換行,最多三行。我是通知本文,可換行,最多三行。</text>" + "</binding>" + "</visual>" + "<audio silent='true'/>" + "</toast>"; // 將字串轉換為 XmlDocument XmlDocument toastDOM = new XmlDocument(); toastDOM.LoadXml(toastXmlString); // 執行個體化 ToastNotification ToastNotification toast = new ToastNotification(toastDOM); // 監聽 ToastNotification 的相關事件 toast.Activated += toast_Activated; toast.Failed += toast_Failed; toast.Dismissed += toast_Dismissed; // 彈出指定的通知 ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier(); toastNotifier.Show(toast); lblStatus.Text += "NotificationSetting: " + toastNotifier.Setting.ToString(); lblStatus.Text += Environment.NewLine; // 顯示此 toast 的 xml lblMsg.Text = toastXmlString; } async void toast_Dismissed(ToastNotification sender, ToastDismissedEventArgs args) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { lblStatus.Text += "Toast.Dismissed: " + args.Reason.ToString(); lblStatus.Text += Environment.NewLine; }); } async void toast_Failed(ToastNotification sender, ToastFailedEventArgs args) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { lblStatus.Text += "Toast.Failed: " + args.ErrorCode.ToString(); lblStatus.Text += Environment.NewLine; }); } async void toast_Activated(ToastNotification sender, object args) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { lblStatus.Text += "Toast.Activated: " + sender.Content.GetXml(); lblStatus.Text += Environment.NewLine; }); } }}