消息通知,是一个应用中必不可少的组成部分。Win10下提供了多种消息通知机制,Toast通知只是其中一种。这篇博文和大家分享一下,如何使用Toast通知。
上图是一个基本的Toast通知,那我们该如何使用它呢?首先大家要知道,Toast 通知是由XML构建的。在Toast通知中提供消息内容及操作(比如回复,取消等)都是在XML中定义。
<toast>
<visual>
<binding template='ToastGeneric'>
<text>Hello World!</text>
<text>This is th 3 Example!</text>
<image src='ms-appx://Assets/Images/photo.jpg' placement='appLogoOverride'/>
</binding>
</visual>
<actions>
<input id='reply' type='text' placeHolderContent='placeHolderContent'/>
<action content='reply' arguments='reply' hint-inputId='reply'/>
</actions>
<audio src='ms-winsoundevent:Notification.Default'/>
</toast>
定义完成Toast通知结构后,接着我们使用 ToastNotification 与 ToastNotificationManager 类完成弹出通知的操作。
ToastNotification notification = new ToastNotification(xml);
ToastNotificationManager.CreateToastNotifier().Show(notification);
Toast 通知使用场景
Toast通知默认支持四种场景,可通过设置 <toast\>
元素下的 scenario
属性。
default
reminder
: 通知会持续显示在屏幕上alarm
: 通知会持续显示在屏幕上并且循环播放声音inComingCall
: 在Moblie下全屏显示等
属性 scenario
默认为 default
, 每一种场景在UI表现与显示模式有所不同。