介绍
背水一战 Windows 10 之 通知(Toast)
- 纯文本 toast
- 短时 toast
- 长时 toast
- 图文 toast
示例
1、本例用于演示如何弹出纯文本 toast,以及如何控制 toast 的显示时长(短时和长时两种)
Notification/Toast/Text.xaml
<Page x:Class="Windows10.Notification.Toast.Text" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.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="10 0 10 10"> <Button Name="buttonShowToast1" Content="显示 toast(短时通知)" Click="buttonShowToast1_Click" Margin="5" /> <Button Name="buttonShowToast2" Content="显示 toast(长时通知)" Click="buttonShowToast2_Click" Margin="5" /> </StackPanel> </Grid> </Page>
Notification/Toast/Text.xaml.cs
/* * 本例用于演示如何弹出纯文本 toast,以及如何控制 toast 的显示时长(短时和长时两种) * 单击 toast 激活 app 后(前台方式激活),如何获取相关信息请参见 Demo.xaml.cs 中的代码 * * * 本例 xml 说明: * activationType - 通过点击 toast 激活 app 时的激活方式,foreground 代表前台方式激活 * launch - 通过点击 toast 激活 app 时,传递给 app 的参数(通过按钮激活时,此参数无效) * duration - short 短时通知(默认值);long 长时通知 * template - 在 uwp 中就 ToastGeneric 一种模板 * text - 每一个新的 text 会另起一行,一行显示不下会自动换行,第一个 text 会高亮显示,最多显示 5 行文本 */ using Windows.Data.Xml.Dom; using Windows.UI.Notifications; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace Windows10.Notification.Toast { public sealed partial class Text : Page { public Text() { this.InitializeComponent(); } // 弹出 toast 通知(短时通知) private void buttonShowToast1_Click(object sender, RoutedEventArgs e) { // 清除本 app 的之前的全部 toast 通知 // ToastNotificationManager.History.Clear(); string toastXml = @" <toast activationType='foreground' launch='Notification-Toast-Text-Arguments 1'> <visual> <binding template='ToastGeneric'> <text>toast - title</text> <text>toast - content</text> <text>""Hololens""引领技术革命浪潮传统的人机交互,主要是通过键盘和触摸,包括并不能被精确识别的语音等。""Hololens""的出现,则给新一代体验更好的人机交互指明道路,并现实了设备的小型化和便携化。</text> </binding> </visual> </toast>"; XmlDocument toastDoc = new XmlDocument(); toastDoc.LoadXml(toastXml); ToastNotification toast = new ToastNotification(toastDoc); ToastNotificationManager.CreateToastNotifier().Show(toast); } // 弹出 toast 通知(长时通知) private void buttonShowToast2_Click(object sender, RoutedEventArgs e) { // 清除本 app 的之前的全部 toast 通知 // ToastNotificationManager.History.Clear(); string toastXml = @" <toast activationType='foreground' duration='long' launch='Notification-Toast-Text-Arguments 2'> <visual> <binding template='ToastGeneric'> <text>toast - title</text> <text>""Hololens""引领技术革命浪潮传统的人机交互,主要是通过键盘和触摸,包括并不能被精确识别的语音等。""Hololens""的出现,则给新一代体验更好的人机交互指明道路,并现实了设备的小型化和便携化。</text> </binding> </visual> </toast>"; XmlDocument toastDoc = new XmlDocument(); toastDoc.LoadXml(toastXml); ToastNotification toast = new ToastNotification(toastDoc); ToastNotificationManager.CreateToastNotifier().Show(toast); } } }
2、本例用于演示如何弹出图文 toast
Notification/Toast/TextAndImage.xaml
<Page x:Class="Windows10.Notification.Toast.TextAndImage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.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="10 0 10 10"> <Button Name="buttonShowToastImageFromAppPackage" Content="显示 toast(图片来自 AppPackage)" Click="buttonShowToastImageFromAppPackage_Click" Margin="5" /> <Button Name="buttonShowToastImageFromAppData" Content="弹出 toast 通知(图片来自 AppData,image 的 hint-crop 为 circle)" Click="buttonShowToastImageFromAppData_Click" Margin="5" /> <Button Name="buttonShowToastImageFromHttp" Content="弹出 toast 通知(图片来自 http,image 的 placement 为 appLogoOverride,image 的 addImageQuery 为 true)" Click="buttonShowToastImageFromHttp_Click" Margin="5" /> </StackPanel> </Grid> </Page>
Notification/Toast/TextAndImage.xaml.cs