消息通知,是一个应用中必不可少的组成部分。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表现与显示模式有所不同。

使用 XML 构建自定义Toast通知

从上述的XML结构中,可以看出Toast通知结构组成:

<toast>
<visual/>
<actions/>
<audio/>
</toast>

下面我们列出一些必要的元素及其必需属性

visual 元素

作用:用于定义Toast通知上展示内容,比如文本或图片。

元素 <binding\>

  • templateToastGeneric 是属性 template 的唯一值。

元素 <text\>

元素 <image\>

  • src : 数据源,支持 http:// | https:// | ms-appx:/// | ms-appdata:///local/ | file:///
  • placement : 图片的显示位置 inline | appLogoOverride (正文 | 替换App图标,Toast通知左上角图标)
  • hint-crop : 图片的剪裁 none | circle (默认 | 圆形切割)

<toast>
<visual>
<binding template='ToastGeneric'>
<text>Hello World!</text>
<text>This is th 2 Example!</text>
<image src='ms-appx:///Assets/Images/photo.jpg' placement='appLogoOverride' hint-crop='circle'/>
<image src='ms-appx:///Assets/Images/demo.jpg' placement='inline'/>
</binding>
</visual>
</toast>

actions 元素

作用:用于定义Toast通知上提供的操作,比如上图中的回复操作。

元素 <input\>

  • id : 元素的 id
  • type: text | selection (文本框 | 选择框)

元素 <selection\>

  • id : 元素的 id
  • content: 选择项的内容

元素 <action\>

  • content
  • argument
  • hint-inputId
  • activationType : foreground | background | protocol | system

<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>
</toast>

<toast>
<visual>
<binding template='ToastGeneric'>
<text>Hello World!</text>
<text>This is th 4 Example!</text>
<image src='ms-appx://Assets/Images/photo.jpg' placement='appLogoOverride'/>
</binding>
</visual>
<actions>
<input id='reply' type='selection'>
<selection content='selection_1' id='s1'/>
<selection content='selection_2' id='s2'/>
</input>
<action content='reply' arguments='reply'/>
<action content='cancel' arguments='cancel'/>
</actions>
</toast>

audio 元素

作用:用于定义Toast通知弹出时所播放的声音。

  • src
  • silent
  • loop

使用 NotificationsExtensions 构建 Toast 通知

如果觉得使用 XML 构建Toast通知太复杂太麻烦,那 NotificationsExtensions 就是为你准备的。

先看一下,NotificationsExtensions 里包含哪些关于 Toast 通知的类。

如果看过上面使用 XML 构建Toast通知的同学,肯定非常熟悉图中的类名,多的就不说了,用一个简单的例子,与大家一起学习 NotificationsExtensions

ToastContent toastContent = new ToastContent();
toastContent.Scenario = ToastScenario.Reminder; //<visual>
ToastVisual toastVisual = new ToastVisual(); //<text>
ToastText toastTitle = new ToastText();
toastTitle.Text = "Hello World ."; ToastText toastTitle2 = new ToastText();
toastTitle2.Text = "This is the 5 Example !";
//</text> //<image>
ToastAppLogo toastAppLogo = new ToastAppLogo();
toastAppLogo.Source = new ToastImageSource("/Assets/Images/photo.jpg"); ToastImage toastImageInline = new ToastImage();
toastImageInline.Source = new ToastImageSource("/Assets/Images/demo.jpg");
//</image> toastVisual.BodyTextLine1 = toastTitle;
toastVisual.BodyTextLine2 = toastTitle2;
toastVisual.AppLogoOverride = toastAppLogo;
toastVisual.InlineImages.Add(toastImageInline);
//</visual> //<actions>
ToastActionsCustom toastActions = new ToastActionsCustom(); ToastTextBox toastTextBox = new ToastTextBox("replyTextBox");
toastTextBox.PlaceholderContent = "输入回复消息"; ToastButton replyButton = new ToastButton("回复","reply");
replyButton.TextBoxId = "replyTextBox"; toastActions.Inputs.Add(toastTextBox);
toastActions.Buttons.Add(replyButton);
//</actions> //<audio>
ToastAudio toastAudio = new ToastAudio();
toastAudio.Src = new Uri("ms-winsoundevent:Notification.Default");
//</audio> toastContent.Visual = toastVisual;
toastContent.Actions = toastActions;
toastContent.Audio = toastAudio; ToastNotification toastNotification = new ToastNotification(toastContent.GetXml());
ToastNotificationManager.CreateToastNotifier().Show(toastNotification);

使用 NotificationsExtensions 构建 Toast 通知是不是更加的方便和直观呢。从上面代码可以看出,我们使用 ToastContent 类构建 Toast 通知,对其属性 Visual , Actions , Audio 赋值,完成后调用 GetXml 方法获取一个 XmlDocument 对象,之后的过程就与原来一样了。

使用工具 Notifications Visualizer

Notifications Visualizer 可以协助我们构建 Toast 通知(当然也可以构建磁贴),它类似于 Visual Studio Designer,提供实时预览的功能,是一个提高开发效率的工具,推荐给大家。

Windows Store 下载: https://www.microsoft.com/en-us/store/apps/notifications-visualizer/9nblggh5xsl1

结束

这篇博客与大家一起学习了如何快速构建 Toast 通知,当然还有一些细节,比如在 Toast 通知的 XML 结构中某些元素的属性没有提及到,如果大家想了解,请参考下面的链接。OK,如何构建大家既然明白了,下一篇博客就该谈一下 Toast 通知的交互问题了,期待吧。

参考链接

自适应和交互式 Toast 通知

[Win10应用开发] 如何使用Windows通知的更多相关文章

  1. Win 10 开发中Adaptive磁贴模板的XML文档结构,Win10 应用开发中自适应Toast通知的XML文档结构

    分享两篇Win 10应用开发的XML文档结构:Win 10 开发中Adaptive磁贴模板的XML文档结构,Win10 应用开发中自适应Toast通知的XML文档结构. Win 10 开发中Adapt ...

  2. 【Win10 应用开发】自适应Toast通知的XML文档结构

    老规矩,在开始之前老周先讲个故事. 话说公元2015年7月20日,VS 2015发布.于是,肯定有人会问老周了,C#6有啥新特性,我学不来啊.学不来的话你应该检讨.老周比较保守地计算一下,学会C# 6 ...

  3. 【Win10应用开发】自定义磁贴通知的排版

    前面老周用了两篇烂文,向大家介绍了Adaptive磁贴的模板使用.那些XML模板已经很强大了,不过,如果你觉得那些排版还不足以满足需求,不妨试试自己来定义磁贴的内容. 其实,Runtime App支持 ...

  4. Win10/UWP开发—使用Cortana语音与App后台Service交互

    上篇文章中我们介绍了使用Cortana调用前台App,不熟悉的移步到:Win10/UWP开发—使用Cortana语音指令与App的前台交互,这篇我们讲讲如何使用Cortana调用App的后台任务,相比 ...

  5. 微软开放技术开发了适用于 Windows Azure 移动服务的开源 Android SDK

     发布于 2014-02-10 作者 陈 忠岳 为进一步实现连接微软与非微软技术的目标,微软开放技术有限公司开发了适用于 Windows Azure 移动服务的 Android SDK,由Scot ...

  6. 在 Visual Studio 2010 中开发和部署 Windows Azure 应用程序

    原文 在 Visual Studio 2010 中开发和部署 Windows Azure 应用程序 在 Visual Studio 2010 中开发和部署 Windows Azure 应用程序 Jim ...

  7. win10应用开发——如何判断应用是在手机上运行还是电脑上运行

    原文:win10应用开发--如何判断应用是在手机上运行还是电脑上运行 在进行uwp应用开发的时候, 有时我们需要知道自己的应用是在手机端运行还是在桌面端运行,那么通过以下的api就可以进行判断: Wi ...

  8. Win10 UWP开发系列:使用VS2015 Update2+ionic开发第一个Cordova App

    安装VS2015 Update2的过程是非常曲折的.还好经过不懈的努力,终于折腾成功了. 如果开发Cordova项目的话,推荐大家用一下ionic这个框架,效果还不错.对于Cordova.PhoneG ...

  9. AndroidStudio开发环境配置-Windows

    Android Studio开发环境配置-Windows 最近突发奇想,开始研究Android开发.开始时使用Eclipse作为开发IDE,结果各种不好使,首先下载和安装SDK,以及不同版本的Imag ...

随机推荐

  1. 菜单下拉效果demo记录

    <!doctype html> <html> <head> <meta http-equiv="Content-Type" content ...

  2. PHP基础知识(一)

    The Basics Comparison operators Comparison operators are an often overlooked aspect of PHP, which ca ...

  3. win7之64位下安装oracle11g遇到问题和不能删除干净的问题

    今天在win7下装了oracle11g 删了又卸 来来回回重启了4.5次,结合网上是解释归纳下几点: 一.win64_11gR2_database_1of2.zip和win64_11gR2_datab ...

  4. 使用gradle构建Android时 版本号versionName中嵌入git提交信息

    为什么要这么做   在应用开发的版本迭代过程中,通过版本号并不能快速定位到所对应的代码,导致在后面分析问题追溯对应版本的代码时比较麻烦.   如果代码是通过git来管理的,git的commit id等 ...

  5. 算法笔记_090:蓝桥杯练习 7-1用宏求球的体积(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 使用宏实现计算球体体积的功能.用户输入半径,系统输出体积.不能使用函数,pi=3.1415926,结果精确到小数点后五位. 样例输入 一个 ...

  6. input输入框禁止显示历史记录

    有时我们在设计网页时不想让表单保存用户输入历史记录,比如一些隐私数据 <input name="test" type="text" id="te ...

  7. 《The Story of My Life》Introductiom - Historical and Literary Context - Education of the Deaf and Blind

    At the time the Story of My Life was published, the idea of a disabled person as an active member of ...

  8. Latex排版:CTeX winEdit 输出“系统找不到指定的文件”的解决办法)

    winEdit输出“系统找不到指定的文件”,这里“指定的文件”是“TeXify.exe”等需要运行的程序,而不是当前需要编译的“.tex”文件.所以,问题的本质就是系统找不到“TeXify.exe”等 ...

  9. OPC UA的监控项、订阅、和通知

    MonitoredItem 每个监控项均指明了要监控的项目(item)和用来发送通知的订阅. item可以是一个节点的属性(node attribute). MonitorItem可以监控一个属性,一 ...

  10. OpenJudge百炼习题解答(C++)--题4074:积水量

    题: 总时间限制: 1000ms       内存限制:65536kB 描写叙述 凹凸不平的地面每当下雨的时候总会积水.如果地面是一维的.每一块宽度都为1,高度是非负整数.那么能够用一个数组来表达一块 ...