Windows 8 Store Apps学习(35) 通知: Toast 详解2013-12-06 cnblogs webabcd介绍重新想象 Windows 8 Store Apps 之 通知Toast - 基本应用参见 http://www.cnblogs.com/webabcd/archive/2013/06/17/3139740.htmlToast - 纯文本 toastToast - 图文 toastToast - toast 的提示音Toast - 按计划弹出 toast示例1、演示纯文本 toast 的 4 个模板Notification/Toast/ToastWithText.xaml
<Pagex:Class="XamlDemo.Notification.Toast.ToastWithText"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"><Grid Background="Transparent"><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="btnTextBodyWrap" Content="TextBodyWrap" Click="btnTextBodyWrap_Click_1" Margin="0 10 0 0" /><Button Name="bntTextHeadingTextBodyWrap" Content="TextHeading TextBodyWrap" Click="bntTextHeadingTextBodyWrap_Click_1" Margin="0 10 0 0" /><Button Name="bntTextHeadingWrapTextBody" Content="TextHeadingWrap TextBody" Click="bntTextHeadingWrapTextBody_Click_1" Margin="0 10 0 0" /><Button Name="bntTextHeadingTextBody" Content="TextHeading TextBody1 TextBody2" Click="bntTextHeadingTextBody_Click_1" Margin="0 10 0 0" /></StackPanel></Grid></Page>
Notification/Toast/ToastWithText.xaml.cs
/* * 演示纯文本 toast 的 4 个模板 * 本示例的 Toast 的 XmlDocument 内容构造器采用一个开源项目,具体代码见:NotificationsExtensions/ToastContent.cs ** XmlDocument GetTemplateContent(ToastTemplateType type) - 获取系统支持的 Toast 模板 * ToastTemplateType.ToastText01, ToastTemplateType.ToastText02, ToastTemplateType.ToastText03, ToastTemplateType.ToastText04 */using NotificationsExtensions.ToastContent;using Windows.UI.Notifications;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;namespace XamlDemo.Notification.Toast{public sealed partial class ToastWithText : Page{public ToastWithText(){this.InitializeComponent();}private void btnTextBodyWrap_Click_1(object sender, RoutedEventArgs e){IToastText01 templateContent = ToastContentFactory.CreateToastText01();templateContent.TextBodyWrap.Text = "我是通知正文,可换行,最多三行。我是通知正文,可换行,最多三行。我是通知正文,可换行,最多三行。";IToastNotificationContent toastContent = templateContent;ToastNotification toast = toastContent.CreateNotification();ToastNotificationManager.CreateToastNotifier().Show(toast);lblMsg.Text = toastContent.GetContent();}private void bntTextHeadingTextBodyWrap_Click_1(object sender, RoutedEventArgs e){IToastText02 templateContent = ToastContentFactory.CreateToastText02();templateContent.TextHeading.Text = "我是通知标题,不可以换行。我是通知标题,不可以换行。";templateContent.TextBodyWrap.Text = "我是通知正文,可换行,最多两行。我是通知正文,可换行,最多两行。";IToastNotificationContent toastContent = templateContent;ToastNotification toast = toastContent.CreateNotification();ToastNotificationManager.CreateToastNotifier().Show(toast);lblMsg.Text = toastContent.GetContent();}private void bntTextHeadingWrapTextBody_Click_1(object sender, RoutedEventArgs e){IToastText03 templateContent = ToastContentFactory.CreateToastText03();templateContent.TextHeadingWrap.Text = "我是通知标题,可换行,最多两行。我是通知标题,可换行,最多两行。";templateContent.TextBody.Text = "我是通知正文,不可以换行。我是通知正文,不可以换行。";IToastNotificationContent toastContent = templateContent;ToastNotification toast = toastContent.CreateNotification();ToastNotificationManager.CreateToastNotifier().Show(toast);lblMsg.Text = toastContent.GetContent();}private void bntTextHeadingTextBody_Click_1(object sender, RoutedEventArgs e){IToastText04 templateContent = ToastContentFactory.CreateToastText04();templateContent.TextHeading.Text = "我是通知标题,不可以换行。我是通知标题,不可以换行。";templateContent.TextBody1.Text = "我是通知正文1,不可以换行。我是通知正文1,不可以换行。";templateContent.TextBody2.Text = "我是通知正文2,不可以换行。我是通知正文2,不可以换行。";IToastNotificationContent toastContent = templateContent;ToastNotification toast = toastContent.CreateNotification();ToastNotificationManager.CreateToastNotifier().Show(toast);lblMsg.Text = toastContent.GetContent();}}}
2、演示图文 toast 的 4 个模板Notification/Toast/ToastWithImageText.xaml
<Pagex:Class="XamlDemo.Notification.Toast.ToastWithImageText"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"><Grid Background="Transparent"><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="btnTextBodyWrap" Content="TextBodyWrap" Click="btnTextBodyWrap_Click_1" Margin="0 10 0 0" /><Button Name="bntTextHeadingTextBodyWrap" Content="TextHeading TextBodyWrap" Click="bntTextHeadingTextBodyWrap_Click_1" Margin="0 10 0 0" /><Button Name="bntTextHeadingWrapTextBody" Content="TextHeadingWrap TextBody" Click="bntTextHeadingWrapTextBody_Click_1" Margin="0 10 0 0" /><Button Name="bntTextHeadingTextBody" Content="TextHeading TextBody1 TextBody2" Click="bntTextHeadingTextBody_Click_1" Margin="0 10 0 0" /></StackPanel></Grid></Page>
Notification/Toast/ToastWithImageText.xaml.cs
/* * 演示图文 toast 的 4 个模板(注:图片不能大于 1024*1024 像素,不能大于 200KB) * 本示例的 Toast 的 XmlDocument 内容构造器采用一个开源项目,具体代码见:NotificationsExtensions/ToastContent.cs ** XmlDocument GetTemplateContent(ToastTemplateType type) - 获取系统支持的 Toast 模板 * ToastTemplateType.ToastImageAndText01, ToastTemplateType.ToastImageAndText02, ToastTemplateType.ToastImageAndText03, ToastTemplateType.ToastImageAndText04 ** 注:图片可以来自程序包内,可以来自 Application Data(仅支持对 local 中图片文件的引用),可以来自一个 http 的远程地址 */using NotificationsExtensions.ToastContent;using System;using Windows.UI.Notifications;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;namespace XamlDemo.Notification.Toast{public sealed partial class ToastWithImageText : Page{public ToastWithImageText(){this.InitializeComponent();}private void btnTextBodyWrap_Click_1(object sender, RoutedEventArgs e){IToastImageAndText01 templateContent = ToastContentFactory.CreateToastImageAndText01();templateContent.TextBodyWrap.Text = "我是通知正文,可换行,最多三行。我是通知正文,可换行,最多三行。我是通知正文,可换行,最多三行。";templateContent.Image.Src = "Assets/Logo.png"; // 用程序包内文件作通知图片templateContent.Image.Alt = "altText";IToastNotificationContent toastContent = templateContent;ToastNotification toast = toastContent.CreateNotification();ToastNotificationManager.CreateToastNotifier().Show(toast);lblMsg.Text = toastContent.GetContent();}private void bntTextHeadingTextBodyWrap_Click_1(object sender, RoutedEventArgs e){IToastImageAndText02 templateContent = ToastContentFactory.CreateToastImageAndText02();templateContent.TextHeading.Text = "我是通知标题,不可以换行。我是通知标题,不可以换行。";templateContent.TextBodyWrap.Text = "我是通知正文,可换行,最多两行。我是通知正文,可换行,最多两行。";templateContent.Image.Src = "ms-appx:///Assets/Logo.png"; // 用程序包内文件作通知图片templateContent.Image.Alt = "altText";IToastNotificationContent toastContent = templateContent;ToastNotification toast = toastContent.CreateNotification();ToastNotificationManager.CreateToastNotifier().Show(toast);lblMsg.Text = toastContent.GetContent();}private void bntTextHeadingWrapTextBody_Click_1(object sender, RoutedEventArgs e){IToastImageAndText03 templateContent = ToastContentFactory.CreateToastImageAndText03();templateContent.TextHeadingWrap.Text = "我是通知标题,可换行,最多两行。我是通知标题,可换行,最多两行。";templateContent.TextBody.Text = "我是通知正文,不可以换行。我是通知正文,不可以换行。";templateContent.Image.Src = "ms-appdata:///local/Logo.png"; // 用 Application Data 内文件作通知图片(注:仅支持 local 中的图片)templateContent.Image.Alt = "altText";IToastNotificationContent toastContent = templateContent;ToastNotification toast = toastContent.CreateNotification();ToastNotificationManager.CreateToastNotifier().Show(toast);lblMsg.Text = toastContent.GetContent();}private void bntTextHeadingTextBody_Click_1(object sender, RoutedEventArgs e){IToastImageAndText04 templateContent = ToastContentFactory.CreateToastImageAndText04();templateContent.TextHeading.Text = "我是通知标题,不可以换行。我是通知标题,不可以换行。";templateContent.TextBody1.Text = "我是通知正文1,不可以换行。我是通知正文1,不可以换行。";templateContent.TextBody2.Text = "我是通知正文2,不可以换行。我是通知正文2,不可以换行。";templateContent.Image.Src = "http://pic.cnblogs.com/avatar/a14540.jpg?id=24173245"; // 用远程文件作通知图片templateContent.Image.Alt = "altText";IToastNotificationContent toastContent = templateContent;ToastNotification toast = toastContent.CreateNotification();ToastNotificationManager.CreateToastNotifier().Show(toast);lblMsg.Text = toastContent.GetContent();}}}
3、演示 Toast 的提示音Notification/Toast/ToastWithSound.xaml
<Pagex:Class="XamlDemo.Notification.Toast.ToastWithSound"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"><Grid Background="Transparent"><StackPanel Margin="120 0 0 0"><TextBox Name="lblMsg" Height="100" TextWrapping="Wrap" AcceptsReturn="True" FontSize="14.667" Margin="0 0 10 0" /><TextBlock Text="通知提示音列表" Margin="0 10 0 0" /><ListBox Name="listBox" SelectionChanged="listBox_SelectionChanged_1" Margin="0 10 10 0"><ListBoxItem Content="Default" /><ListBoxItem Content="Mail" /><ListBoxItem Content="SMS" /><ListBoxItem Content="IM" /><ListBoxItem Content="Reminder" /><ListBoxItem Content="LoopingCall" /><ListBoxItem Content="LoopingCall2" /><ListBoxItem Content="LoopingAlarm" /><ListBoxItem Content="LoopingAlarm2" /><ListBoxItem Content="Silent" /></ListBox></StackPanel></Grid></Page>