介紹
重新想象 Windows 8 Store Apps 之 通知
Toast - 基本應用參見 http://www.cnblogs.com/webabcd/archive/2013/06/17/3139740.html
Toast - 純文本 toast
Toast - 圖文 toast
Toast - toast 的提示音
Toast - 按計劃彈出 toast
示例
1、演示純文本 toast 的 4 個模板
Notification/Toast/ToastWithText.xaml
<Page
x: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
<Page
x: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
<Page
x: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>
Notification/Toast/ToastWithSound.xaml.cs
/*
* 演示 Toast 的提示音
*
* 目前支持的 Toast 提示音共有以下幾種:
* Default, Mail, SMS, IM, Reminder, LoopingCall, LoopingCall2, LoopingAlarm, LoopingAlarm2,
Silent
*/
using NotificationsExtensions.ToastContent;
using System;
using Windows.UI.Notifications;
using Windows.UI.Xaml.Controls;
namespace XamlDemo.Notification.Toast
{
public sealed partial class ToastWithSound : Page
{
public ToastWithSound()
{
this.InitializeComponent();
}
private void listBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
string audioType = (listBox.SelectedItem as ListBoxItem).Content.ToString();
IToastText02 toastContent = ToastContentFactory.CreateToastText02();
toastContent.TextHeading.Text = "Sound:";
toastContent.TextBodyWrap.Text = audioType;
toastContent.Audio.Content = (ToastAudioContent)Enum.Parse(typeof
(ToastAudioContent), audioType);
/*
* LoopingCall, LoopingCall2, LoopingAlarm, LoopingAlarm2 這 4 種提示音僅針對長時通知,且需指定為循環提示音
*/
if (audioType == "LoopingCall" || audioType == "LoopingCall2" || audioType == "LoopingAlarm" || audioType == "LoopingAlarm2")
{
toastContent.Duration = ToastDuration.Long;
toastContent.Audio.Loop = true;
}
ToastNotification toast = toastContent.CreateNotification();
ToastNotificationManager.CreateToastNotifier().Show(toast);
lblMsg.Text = toastContent.GetContent();
}
}
}
4、演示如何按計劃顯示 Toast 通知
Notification/Toast/ScheduledToast.xaml
<Page
x:Class="XamlDemo.Notification.Toast.ScheduledToast"
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">
<!--顯示當前 app 的全部 ScheduledToastNotification 對象列表-->
<ListBox Name="listBox" Width="800" Height="300" HorizontalAlignment="Left">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ToastId}" VerticalAlignment="Center" />
<TextBlock Text="{Binding Text}" Margin="15 0 0 0" VerticalAlignment="Center" />
<HyperlinkButton Name="btnRemove" Content="刪除此 ScheduledToastNotification" Tag="{Binding ToastId}" Margin="15 0 0 0" Click="btnRemove_Click_1"
/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Name="btnScheduledToast" Content="ScheduledToastNotification 的 Demo(3 秒後彈出 Toast 通知,然後每隔 60 秒再彈出一次,共重復 5 次)" Click="btnScheduledToast_Click_1"
Margin="0 10 0 0" />
</StackPanel>
</Grid>
</Page>
Notification/Toast/ScheduledToast.xaml.cs
/*
* 演示如何按計劃顯示 Toast 通知
*
* ScheduledToastNotification - 按計劃顯示 Toast 通知
* Content - Toast 的內容,XmlDocument 類型的數據,只讀,其需要在構造函數中指定
* DeliveryTime - 顯示 Toast 通知的時間,只讀,其需要在構造函數中指定
* SnoozeInterval - 循環顯示 Toast 通知的間隔時長(60 秒 - 60 分之間),只讀,其需要在構造函數中指定
* MaximumSnoozeCount - 循環的最大次數(1 - 5 次)
* Id - ScheduledToastNotification 的標識
*
* ToastNotifier - Toast 通知器
* AddToSchedule() - 將指定的 ScheduledToastNotification 添加到計劃列表
* RemoveFromSchedule() - 從計劃列表中移除指定的 ScheduledToastNotification
* GetScheduledToastNotifications() - 獲取當前 app 的全部 ScheduledToastNotification 集合
*/
using NotificationsExtensions.ToastContent;
using System;
using System.Collections.Generic;
using Windows.UI.Notifications;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace XamlDemo.Notification.Toast
{
public sealed partial class ScheduledToast : Page
{
public ScheduledToast()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
ShowScheduledToasts();
}
// 添加指定的 ScheduledToastNotification 到計劃列表中
private void btnScheduledToast_Click_1(object sender, RoutedEventArgs e)
{
IToastText02 toastContent = ToastContentFactory.CreateToastText02();
toastContent.TextHeading.Text = "ScheduledToastNotification Demo";
toastContent.TextBodyWrap.Text = "received: " + DateTime.Now.ToString("hh:mm:ss");
// 3 秒後顯示 Toast,然後每隔 60 秒再顯示一次 Toast(循環顯示 5 次)
ScheduledToastNotification toast = new ScheduledToastNotification(toastContent.GetXml(), DateTime.Now.AddSeconds(3), TimeSpan.FromSeconds(60), 5);
string toastId = new Random().Next(1000, 10000).ToString();
toast.Id = toastId;
// 將指定的 ScheduledToastNotification 添加進計劃列表
ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier();
toastNotifier.AddToSchedule(toast);
ShowScheduledToasts();
}
// 顯示當前 app 的全部 ScheduledToastNotification 列表
private void ShowScheduledToasts()
{
List<MyScheduledToast> dataSource = new List<MyScheduledToast>();
// 獲取當前 app 計劃列表中的全部 ScheduledToastNotification 對象列表
ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier();
IReadOnlyList<ScheduledToastNotification> scheduledToasts = toastNotifier.GetScheduledToastNotifications();
int toastCount = scheduledToasts.Count;
for (int i = 0; i < toastCount; i++)
{
ScheduledToastNotification toast = scheduledToasts[i];
dataSource.Add(new MyScheduledToast()
{
ToastId = toast.Id,
Text = toast.Content.GetElementsByTagName("text")[0].InnerText
});
}
listBox.ItemsSource = dataSource;
}
// 根據 ToastId 刪除指定的 ScheduledToastNotification
private void btnRemove_Click_1(object sender, RoutedEventArgs e)
{
string toastId = (string)(sender as FrameworkElement).Tag;
// 獲取當前 app 計劃列表中的全部 ScheduledToastNotification 對象列表
ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier();
IReadOnlyList<ScheduledToastNotification> scheduledToasts = toastNotifier.GetScheduledToastNotifications();
int toastLength = scheduledToasts.Count;
for (int i = 0; i < toastLength; i++)
{
if (scheduledToasts[i].Id == toastId)
{
// 從計劃列表中移除指定的 ScheduledToastNotification 對象
toastNotifier.RemoveFromSchedule(scheduledToasts[i]);
ShowScheduledToasts();
break;
}
}
}
class MyScheduledToast
{
public string ToastId { get; set; }
public string Text { get; set; }
}
}
}
OK
[源碼下載]:http://files.cnblogs.com/webabcd/Windows8.rar