Toast 通知是 Windows 8 應用程式主動觸達使用者的關鍵管道。本文詳細介紹 8 種內建模板(4 種純文字、4 種圖文)、自訂提示音、以及排程顯示機制,並提供完整可執行的程式碼範例。
對應 ToastTemplateType.ToastText01 至 ToastText04,支援標題、內文換行與多行顯示的不同組合。
<Page x:Class="XamlDemo.Notification.Toast.ToastWithText" xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Notification.Toast" xmlns:d="https://schemas.microsoft.com/expression/blend/2008" xmlns:mc="https://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>
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();
}
}
}
對應 ToastTemplateType.ToastImageAndText01 至 ToastImageAndText04。圖片來源支援應用程式封裝內、Application Data (local) 與 HTTP/HTTPS 遠端 URL,大小限制 1024x1024 像素、200KB 以內。
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";
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 = "https://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();
}
}
}
系統內建 10 種提示音:Default、Mail、SMS、IM、Reminder、LoopingCall、LoopingCall2、LoopingAlarm、LoopingAlarm2、Silent。長時間通知需設定 Duration = ToastDuration.Long 並啟用 Loop = true。
<Page x:Class="XamlDemo.Notification.Toast.ToastWithSound" xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Notification.Toast" xmlns:d="https://schemas.microsoft.com/expression/blend/2008" xmlns:mc="https://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>
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);
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();
}
}
}
ScheduledToastNotification 支援指定顯示時間、重複間隔 (SnoozeInterval 60 秒至 60 分鐘) 與最大重複次數 (MaximumSnoozeCount 1-5 次)。透過 ToastNotifier.AddToSchedule 加入排程清單,GetScheduledToastNotifications 查詢,RemoveFromSchedule 移除。
<Page x:Class="XamlDemo.Notification.Toast.ScheduledToast" xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Notification.Toast" xmlns:d="https://schemas.microsoft.com/expression/blend/2008" xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
<Grid Background="Transparent">
<StackPanel Margin="120 0 0 0">
<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>
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();
}
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");
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;
ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier();
toastNotifier.AddToSchedule(toast);
ShowScheduledToasts();
}
private void ShowScheduledToasts()
{
List<MyScheduledToast> dataSource = new List<MyScheduledToast>();
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;
}
private void btnRemove_Click_1(object sender, RoutedEventArgs e)
{
string toastId = (string)(sender as FrameworkElement).Tag;
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)
{
toastNotifier.RemoveFromSchedule(scheduledToasts[i]);
ShowScheduledToasts();
break;
}
}
}
class MyScheduledToast
{
public string ToastId { get; set; }
public string Text { get; set; }
}
}
}