一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Win10 应用开发中Toast通知的XML文档结构教程

时间:2015-07-23 编辑:简简单单 来源:一聚教程网

在以前8.1时候的Toast通知方式,到了Win 10中依然支持,而且UWP API中也包括了对应的API协定。所以,你仍然可以使用8.1的通知方式,即通过ToastNotificationManager.GetTemplateContent(ToastTemplateType)方法来获取对应的通知模板,然后用XmlDocument对象来加载和修改XML,当然你也可以直接构造XML文档,然后用XmlDocument对象来加载。

还有就是操作中心,其实它和Toast是一伙的,即我们不需要用专门的API去操作它,因为Toast通知本身就可以显示在操作中心里面。

到了Win10 app,Toast通知的模板不再划分为N多种,而只有一种,统一命名为ToastGeneric,也就是通用型通知,这种新的Toast通知也称Adaptive Toast通知,就翻译为自适应吧。

整个XML文档的结构如下:


   
      
            .....
             ........
            
             ........
      

   

   



根节点当然是toast的了,这个没什么可以解释的,就像磁贴通知的根节点是tile一样。

根节点下包含两个家伙,实际上这两哥们儿已经把toast通知的界面结构描述出来了。

第一部分:visual,就是通知的内容区域,可以包含文本和图像。

第二部分:actions,即在通知的下方可以显示按钮、输入框、下拉选择列表这些可以让用户操作的控件。就像WP10中的快速回复短信一样。收到短信后,会在手机屏幕顶部显示toast通知,然后用户点一下下拉符号,可以直接在toast通知上输入回复信息,然后发送。

在Toast元素下,还有一个audio元素,用来配置显示Toast通知时的声音,audio元素的使用和8.1一样。

Visual元素下需要放一个binding元素,这和以前一样。而重点是binding元素的template属性,这个跟以前不同,在UWP应用中,template统一叫做“ToastGeneric”,你不用再去考虑用哪个模板了,现在就一个名字。

同样地,binding元素下面可以包含两个元素:

:表示一行文本,两个text元素则表示两行文本,三个就表示三行,通常第一个text元素作为通知的标题。至于说最多能添加多少个text元素,现在还不能确定,通常不会超个四行,毕竟通知内容不应太长。如果想在通知的内容中添加空白行,可以直接写一个(空的text元素)。

:表示一个图像。其他属性和8.1一样,我这里重点说一下两个新的属性。

>>  a、placement,这是可选属性,如果设置为inline,则表示图像和文本内联到一起;如果设置为appLogoOverride,就把应用的默认图标替换,图像显示在通知的左上角。

另外,官方博客里有这么一句话:You can have up to 1 image for each placement value.  placement属性的各个取值只能出现一次,如果有一个image元素的placement属性为inline,那么如果通知中还有image元素,那另外的元素的placement属性只能为appLogoOverride了。

>> b、hint-crop,这也是个可选的属性,默认值为none,如果设置为circle,则图标会变成圆形,如果你习惯于正方形图标,那就不要设置这个属性了。

 好了,不讲太多了,免得大家吸收不了。关于actions就留到下一篇烂文再说吧。下面给大家直观地展示一下新Toast通知吧。

先给大家演示一个带有三行文本和一个图像的Toast通知。看代码:

string xml = "" +
                "" +
                    "" +
                        "高端应用" +
                        "看,桃花开了。" +
                        "" +
                        "这桃花好看吧?" +
                    "
" +
                "
" +
             "
";
// 创建XML文档
XmlDocument doc = new XmlDocument();
// 加载XML
doc.LoadXml(xml);
// 创建通知实例
ToastNotification notification = new ToastNotification(doc);
// 显示通知
ToastNotifier nt = ToastNotificationManager.CreateToastNotifier();
nt.Show(notification);


得到的Toast通知如下图所示。

 


然后打开通知中心,会看到该通知,点击通知下方的“向下”箭头,可以看到通知的全部内容。

 


如果通知显示不出来,请检查设置里面是否开启应用通知。如下图。

 


想不想看看在手机上运行如何?

 


打开手机的通知中心,也可以看到刚才的通知。

 


下面看看纯文本的Toast通知。

string xml = "" +
                "" +
                    "" +
                        "来两句" +
                        "风声,雨声,读书声,声声入耳" +
                        "国事,家事,天下事,事事关心" +
                    "
" +
                "
" +
             "
";
// 创建XML文档
XmlDocument doc = new XmlDocument();
// 加载XML
doc.LoadXml(xml);
// 创建通知实例
ToastNotification notification = new ToastNotification(doc);
// 显示通知
ToastNotifier nt = ToastNotificationManager.CreateToastNotifier();
nt.Show(notification);

通知效果如下。


深入浅出WP8.1(Runtime)]Toast通知讲解

9.1 Toast通知

Toast通知是在屏幕最顶上弹出来的临时通知,是Windows Phone通用的弹出式短暂的通知,默认的系统消息都是采用Toast通知的形式,比如当你手机收到短信的时候,在手机的顶端弹出的消息就是Toast通知,点击该通知你可以直接进入短信的详情页面,通知显示的时间是7秒钟,7秒钟后会自动消失,如果你想快速关闭通知,可以采用在Toast通知上面向右滑动的手势便可以快速地关闭掉当前的Toast通知。除了系统使用这样的Toast通知之外,第三方的应用程序也是可以使用这种通知的形式,Toast通知不仅仅可以在打开应用程序的时候弹出,也可以在应用程序关闭的情况进行定时通知或者推送通知来进行发送,这也是Toast通知的最大的魅力所在。Toast通知只应该用于用户特别感兴趣的信息,通常涉及某种形式的用户选择。因此,收到IM聊天请求和用户选择接收的信息都是不错的选择。但是,当你考虑使用Toast通知时,你必须认识到非常重要的一点,由于它的短暂性或由于用户设置,用户可能错过而未看到它。Toast通知专为与锁屏提醒、磁贴通知及应用中UI结合使用而设计,旨在让用户即时了解你应用中的相关事件或项目。Toast通知的实现还会分为两种形式,一种是在应用程序本地实现,另外一种是在云端实现,进行推送,那么我们这一小节主要是讲解在应用程序本地实现的Toast通知,在云端实现的Toast通知,可以参考第12章推送通知的内容讲解。

9.1.1 创建一个通知消息

你的应用要想通过Toast通知通信,必须在应用的清单文件Package.appxmanifest中声明它支持 Toast,否调用Toast通知相关的API将不会生效。在Package.appxmanifest的可视化界面中,找到“Application”->“Notifications”->“Toast capable”,然后设置为“Yes”。打开Package.appxmanifest的代码视图文件,可以看到m3:VisualElements元素的ToastCapable属性设置为true,代码如下所示:



 

    ……

 




添加了Toast通知的权限之后,我们来看一段创建Toast通知并弹出的代码示例:

// 获取Tosat通知的模板

XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);

// 找到模板中“'text'”元素,然后添加通知的内容

XmlNodeList elements = toastXml.GetElementsByTagName("text");

elements[0].AppendChild(toastXml.CreateTextNode("A sample toast"));

// 通过通知的模板创建一个Toast通知

ToastNotification toast = new ToastNotification(toastXml);

// 弹出通知

ToastNotificationManager.CreateToastNotifier().Show(toast);

下面我们再根据上面的代码来一步步地都讲解Toast通知的编程步骤:

(1)Toast通知的模板

每个Toast通知的格式都会对应着一个XML的模板,在我们创建一个Toast通知对象之前,我们首先需要选择Toast通知的模板。Toast通知的模板由ToastTemplateType来进行描述,可以通过Toast通知管理类ToastNotificationManager类的静态方法GetTemplateContent来获取对应的Toast通知模板。在Windows Phone的应用程序里面主要会用到ToastImageAndText01和ToastImageAndText02这两种类型的模板。

1)ToastText01模板表示是一个最简单的Toast通知模板,只有通知的内容信息,它的XML格式如下所示:



   

       

            bodyText

       


   




2)ToastText02模板表示是包含消息头和消息体的模板,消息头是一个加粗文本字符串,消息头和消息体会使用空格隔开,它的XML格式如下所示:



   

       

            headlineText

            bodyText

       


   




(2)添加Toast通知的内容

获取了Toast通知的模板对象之后,我们可以通过XML对象XmlDocument对象的相关属性和方法来修改XML的内容,从而实现在Toast通知的XML模板上添加消息的内容信息。

(3)创建Toast通知的对象

添加好Toast通知的内容之后,我们就可以使用XmlDocument对象来初始化一个Toast通知对象,这时候可以使用ToastNotification类的构造方法ToastNotification(XmlDocument content)方法来进行初始化,这也是Toast通知唯一的构造方法。

(4)弹出Toast通知

弹出Toast通知可以使用ToastNotifier类的Show方法,ToastNotifier类是表示Toast通知的通知操作管理器,使用该类可以实现获取Toast列表,打开Toast通知,取下Toast通知等操作。

9.1.2 定期 Toast 通知

Toast通知不仅仅可以在应用程序运行的时候弹出,还可以在应用程序离开前台的时候弹出,这时候可以使用定期Toast来实现。定期Toast通知就是通过预设未来的一个时间,在这个时间点上弹出Toast通知,如果应用程序这时候不在前台运行,Toast通知也可以运行,用户点击Toast通知的时候可以直接进入当前的应用程序。

ScheduledToastNotification类表示是定期Toast通知的信息类,你可以使用构造方法ScheduledToastNotification(XmlDocument content, DateTimeOffset deliveryTime)方法来创建一个ScheduledToastNotification对象,然后添加到Toast通知的定时计划里面,其中content参数表示是消息的XML内容,deliveryTime表示是消息弹出的时间。示例代码如下所示:

// 创建一个ToastText02的消息模板

XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);

// 获取XML模板的text元素

XmlNodeList toastNodeList = toastXml.GetElementsByTagName("text");

// 设置通知头信息

toastNodeList.Item(0).AppendChild(toastXml.CreateTextNode("Toast title"));

// 设置通知体信息

toastNodeList.Item(1).AppendChild(toastXml.CreateTextNode("Toast content"));

// 获取一个距离现在还有10秒钟的时间点

DateTime startTime = DateTime.Now.AddSeconds(10);

// 使用XML模板和通知的时间创建一个ScheduledToastNotification对象

ScheduledToastNotification recurringToast = new ScheduledToastNotification(toastXml, startTime);

// 设置通知的ID

recurringToast.Id = "ScheduledToast1";

// 把定时Toast通知添加到通知计划里面

ToastNotificationManager.CreateToastNotifier().AddToSchedule(recurringToast);

9.1.3 实例演示:Toast通知

下面给Toast通知的示例:实现ToastText01和ToastText02两种模板以及定时通知。

代码清单9-1:Toast通知(第9章\Examples_9_1)


MainPage.xaml文件主要代码
------------------------------------------------------------------------------------------------------------------

   
   
   
   
   



MainPage.xaml.cs文件主要代码
------------------------------------------------------------------------------------------------------------------
// 弹出ToastText01模板的Toast通知
private void toastText01_Click(object sender, RoutedEventArgs e)
{
    XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
    XmlNodeList elements = toastXml.GetElementsByTagName("text");
    elements[0].AppendChild(toastXml.CreateTextNode("Hello Windows Phone 8.1"));
    ToastNotification toast = new ToastNotification(toastXml);
    toast.Activated += toast_Activated;
    toast.Dismissed += toast_Dismissed;
    toast.Failed += toast_Failed;
    ToastNotificationManager.CreateToastNotifier().Show(toast);
}
// 弹出ToastText02模板的Toast通知
private void toastText02_Click(object sender, RoutedEventArgs e)
{
    XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
    XmlNodeList elements = toastXml.GetElementsByTagName("text");
    elements[0].AppendChild(toastXml.CreateTextNode("WP8.1"));
    elements[1].AppendChild(toastXml.CreateTextNode("Hello Windows Phone 8.1"));
    ToastNotification toast = new ToastNotification(toastXml);
    toast.Activated += toast_Activated;
    toast.Dismissed += toast_Dismissed;
    toast.Failed += toast_Failed;
    ToastNotificationManager.CreateToastNotifier().Show(toast);
}
// 直接使用XML字符串来拼接出ToastText02模板的Toast通知
private void toastXML_Click(object sender, RoutedEventArgs e)
{
    string toastXmlString = ""
    + ""
    + ""
    + "WP8.1"
    + "" + "Received: " + DateTime.Now.ToLocalTime() + ""
    + "
"
    + "
"
    + "
";
    XmlDocument toastXml = new XmlDocument();
    toastXml.LoadXml(toastXmlString);
    ToastNotification toast = new ToastNotification(toastXml);
    toast.Activated += toast_Activated;
    toast.Dismissed += toast_Dismissed;
    toast.Failed += toast_Failed;
    ToastNotificationManager.CreateToastNotifier().Show(toast);
}
// Toast通知弹出失败的事件
async void toast_Failed(ToastNotification sender, ToastFailedEventArgs args)
{
    await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            info.Text = "Toast通知失败:" + args.ErrorCode.Message;
        });       
}
// Toast通知消失的事件,当通知自动消失或者手动取消会触发该事件
async void toast_Dismissed(ToastNotification sender, ToastDismissedEventArgs args)
{
    await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            info.Text = "Toast通知消失:" + args.Reason.ToString();
        });          
}
// Toast通知激活的事件,当通知弹出时,点击通知会触发该事件
async void toast_Activated(ToastNotification sender, object args)
{
    await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        info.Text = "Toast通知激活";
    });
}
// 定时Toast通知
private void scheduledToast_Click(object sender, RoutedEventArgs e)
{
    XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
    XmlNodeList toastNodeList = toastXml.GetElementsByTagName("text");
    toastNodeList.Item(0).AppendChild(toastXml.CreateTextNode("Toast title"));
    toastNodeList.Item(1).AppendChild(toastXml.CreateTextNode("Toast content"));
    DateTime startTime = DateTime.Now.AddSeconds(3);
    ScheduledToastNotification recurringToast = new ScheduledToastNotification(toastXml, startTime);
    recurringToast.Id = "ScheduledToast1";
    ToastNotificationManager.CreateToastNotifier().AddToSchedule(recurringToast);
}


热门栏目