Welcome

首页 / 软件开发 / .NET编程技术 / Windows 8 Store Apps学习(34) 通知: Toast Demo, Tile Demo, Badge Demo

Windows 8 Store Apps学习(34) 通知: Toast Demo, Tile Demo, Badge Demo2013-12-06 cnblogs webabcd介绍

重新想象 Windows 8 Store Apps 之 通知

Toast - 通知的应用

Tile - 瓷贴的应用

Badge - 徽章的应用

Badge - 轮询服务端以更新 Badge 通知

示例

1、演示 toast 的基 本应用

Notification/Toast/Demo.xaml

<Pagex: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.csIToastText01 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 的 xmllblMsg.Text = toastContent.GetContent();}// 弹出长时通知private void btnLongToast_Click_1(object sender, RoutedEventArgs e){// 手工构造 Toast 通知的 xmlvar toastXmlString = "<toast duration="long">" + "<visual version="1">" + "<binding template="ToastText01">" + "<text id="1">我是通知正文,可换行,最多三行。我是通知正文,可换行,最多三行。我是通知正文,可换行,最多三行。</text>" + "</binding>" + "</visual>" + "<audio silent="true"/>" + "</toast>";// 将字符串转换为 XmlDocumentXmlDocument toastDOM = new XmlDocument();toastDOM.LoadXml(toastXmlString);// 实例化 ToastNotificationToastNotification 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 的 xmllblMsg.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;});}}}