消息通知,是一个应用中必不可少的组成部分。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. android开发笔记之Volley (1)

    1. volley的简介 Volley is an HTTP library that makes networking for Android apps easier and most import ...

  2. postgresql 如何导入sql文件

    (1)不能使用pgadmin 执行Copy语句,目前客户端还不支持这种写法. (2)打开sql shell,执行如下操作 \i C:/Users/Peter_Youny/Desktop/ischool ...

  3. JRE与JVM、JDK的区别

    JRE与JVM.JDK的区别 一. 详细介绍1.JVM -- java virtual machine JVM就是我们常说的java虚拟机,它是整个java实现跨平台的 最核心的部分,所有的java程 ...

  4. HDOJ 3944 DP?

    尽量沿着边走距离最短.化减后 C(n+1,k)+ n - k, 预处理阶乘,Lucas定理组合数取模 DP? Time Limit: 10000/3000 MS (Java/Others)    Me ...

  5. 001-使用idea开发环境安装部署,npm工具栏,脚本运行

    一.概述 参看官方文档:https://ant.design/docs/spec/introduce-cn 其中包含了设计价值观.设计原则.视觉.模式.可视化.动态等. 其中Ant Design 的 ...

  6. ES6 对象扩展

    1.属性和变量可以简写 let birth = '2000/01/01'; const Person = { name: '张三', //等同于birth: birth birth, // 等同于he ...

  7. 2 自己编写:AppDelegate,CCApplication,CCApplicationProtocol

    1 CCApplicationProtocol.h /* * CCApplicationProtocol.h * *  Created on: 2014年10月19日 *      Author: t ...

  8. js 获取当前时间并格式化

      js 获取当前时间并格式化 CreateTime--2018年2月7日11:04:16 Author:Marydon 方式一 /** * 获取系统当前时间并格式化 * @returns yyyy- ...

  9. 08-hibernate注解-总结

    直接贴过来了: 1, 2,

  10. openSession() 与 getCurrentSession() 有何不同和关联呢?

    在 SessionFactory 启动的时候, Hibernate 会根据配置创建相应的 CurrentSessionContext ,在getCurrentSession() 被调用的时候,实际被执 ...