Windows Store apps开发[46]当用户点击Toast通知时,我们可以做些什么

来源:互联网 发布:东华大学网络教育 编辑:程序博客网 时间:2024/04/27 16:26

注:本文由BeyondVincent(破船)原创首发

        转载请注明出处:BeyondVincent(破船)@DevDiv.com



更多内容请查看下面的帖子


[DevDiv原创]Windows 8 开发Step by Step


小引

在之前的博文中,我曾对Toast通知进行了介绍,如下链接,建议不清楚Toast通知的可以去看看


Windows Store apps[9]通知概述(Toast,Tile和Badge)

 

Windows Store apps开发[10]通知使用(Toast,Tile和Badge)



在这篇文章中,我将对Toast通知进行深入的介绍:如何让Toast通知携带参数,并且在程序中如何获取Toast通知的携带的参数。

这个主题在有些时候非常有用:当用户点击Toast通知时,根据Toast通知的内容,直接使程序打开某个特定的画面。


Toast通知的数据走向步骤

我们先来看看Toast通知的数据走向步骤

1、程序或者web service创建并发送一个包含launch字符串的toast通知

2、toast通知到达

3、用户选择toast通知(单击或者触摸)

4、OnLaunched事件被触发

5、在程序的OnLaunched处理函数中读取launch字符串

6、程序根据launch字符串做出响应的处理(打开指定画面、进行数据请求等)


如何创建并发送一个带launch字符串的toast通知

看下面的代码:注意在这里为了创建toast的方便:使用了NotificationsExtensions,关于NotificationsExtensions的使用,参考下面的链接:


使用 NotificationsExtensions 填充锁屏提醒、磁贴和 Toast 模板


private void sendToast(object sender, RoutedEventArgs e){    IToastImageAndText01 toastContent = ToastContentFactory.CreateToastImageAndText01();    toastContent.Launch = "this is launch string test";    toastContent.TextBodyWrap.Text = "破船,恭喜你,中500W!";    toastContent.Image.Src = "/Assets/98_avatar_big.jpg";    ToastNotificationManager.CreateToastNotifier().Show(toastContent.CreateNotification());}

在程序中获得launch字符串

当用户点击或者触摸Toast通知,都会启动相应的程序。并触发OnLaunched事件。
一定要注意:如果在toast通知中没有包含launch字符串,那么OnLaunched是不会被触发的。

下面的代码演示了如何在程序中获得launch字符串
protected override void OnLaunched(LaunchActivatedEventArgs args){    string launchString = args.Arguments    ....}

运行效果

1、发送toast通知



2、触发toast通知


3、获取launch字符串


代码下载地址:
http://www.devdiv.com/thread-147671-1-1.html




开发花絮 o(∩_∩)o









原创粉丝点击