设为首页 收藏本站
查看: 711|回复: 0

重新想象 Windows 8 Store Apps (35)

[复制链接]
YunVN网友  发表于 2015-5-22 07:14:20 |阅读模式
  [源码下载]




重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解  
作者:webabcd

介绍
重新想象 Windows 8 Store Apps 之 通知


  • Toast - 基本应用参见 http://www.iyunv.com/webabcd/archive/2013/06/17/3139740.html
  • Toast - 纯文本 toast
  • Toast - 图文 toast
  • Toast - toast 的提示音
  • Toast - 按计划弹出 toast
  
示例
1、演示纯文本 toast 的 4 个模板
Notification/Toast/ToastWithText.xaml















  Notification/Toast/ToastWithText.xaml.cs



/*
* 演示纯文本 toast 的 4 个模板
* 本示例的 Toast 的 XmlDocument 内容构造器采用一个开源项目,具体代码见:NotificationsExtensions/ToastContent.cs
*
* XmlDocument GetTemplateContent(ToastTemplateType type) - 获取系统支持的 Toast 模板
*     ToastTemplateType.ToastText01, ToastTemplateType.ToastText02, ToastTemplateType.ToastText03, ToastTemplateType.ToastText04
*/
using NotificationsExtensions.ToastContent;
using Windows.UI.Notifications;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace XamlDemo.Notification.Toast
{
public sealed partial class ToastWithText : Page
{
public ToastWithText()
{
this.InitializeComponent();
}
private void btnTextBodyWrap_Click_1(object sender, RoutedEventArgs e)
{
IToastText01 templateContent = ToastContentFactory.CreateToastText01();
templateContent.TextBodyWrap.Text = "我是通知正文,可换行,最多三行。我是通知正文,可换行,最多三行。我是通知正文,可换行,最多三行。";
IToastNotificationContent toastContent = templateContent;
ToastNotification toast = toastContent.CreateNotification();
ToastNotificationManager.CreateToastNotifier().Show(toast);
lblMsg.Text = toastContent.GetContent();
}
private void bntTextHeadingTextBodyWrap_Click_1(object sender, RoutedEventArgs e)
{
IToastText02 templateContent = ToastContentFactory.CreateToastText02();
templateContent.TextHeading.Text = "我是通知标题,不可以换行。我是通知标题,不可以换行。";
templateContent.TextBodyWrap.Text = "我是通知正文,可换行,最多两行。我是通知正文,可换行,最多两行。";
IToastNotificationContent toastContent = templateContent;
ToastNotification toast = toastContent.CreateNotification();
ToastNotificationManager.CreateToastNotifier().Show(toast);
lblMsg.Text = toastContent.GetContent();
}
private void bntTextHeadingWrapTextBody_Click_1(object sender, RoutedEventArgs e)
{
IToastText03 templateContent = ToastContentFactory.CreateToastText03();
templateContent.TextHeadingWrap.Text = "我是通知标题,可换行,最多两行。我是通知标题,可换行,最多两行。";
templateContent.TextBody.Text = "我是通知正文,不可以换行。我是通知正文,不可以换行。";
IToastNotificationContent toastContent = templateContent;
ToastNotification toast = toastContent.CreateNotification();
ToastNotificationManager.CreateToastNotifier().Show(toast);
lblMsg.Text = toastContent.GetContent();
}
private void bntTextHeadingTextBody_Click_1(object sender, RoutedEventArgs e)
{
IToastText04 templateContent = ToastContentFactory.CreateToastText04();
templateContent.TextHeading.Text = "我是通知标题,不可以换行。我是通知标题,不可以换行。";
templateContent.TextBody1.Text = "我是通知正文1,不可以换行。我是通知正文1,不可以换行。";
templateContent.TextBody2.Text = "我是通知正文2,不可以换行。我是通知正文2,不可以换行。";
IToastNotificationContent toastContent = templateContent;
ToastNotification toast = toastContent.CreateNotification();
ToastNotificationManager.CreateToastNotifier().Show(toast);
lblMsg.Text = toastContent.GetContent();
}
}
}
  
2、演示图文 toast 的 4 个模板
Notification/Toast/ToastWithImageText.xaml














  Notification/Toast/ToastWithImageText.xaml.cs



/*
* 演示图文 toast 的 4 个模板(注:图片不能大于 1024*1024 像素,不能大于 200KB)
* 本示例的 Toast 的 XmlDocument 内容构造器采用一个开源项目,具体代码见:NotificationsExtensions/ToastContent.cs
*
* XmlDocument GetTemplateContent(ToastTemplateType type) - 获取系统支持的 Toast 模板
*     ToastTemplateType.ToastImageAndText01, ToastTemplateType.ToastImageAndText02, ToastTemplateType.ToastImageAndText03, ToastTemplateType.ToastImageAndText04
*     
* 注:图片可以来自程序包内,可以来自 Application Data(仅支持对 local 中图片文件的引用),可以来自一个 http 的远程地址
*/
using NotificationsExtensions.ToastContent;
using System;
using Windows.UI.Notifications;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace XamlDemo.Notification.Toast
{
public sealed partial class ToastWithImageText : Page
{
public ToastWithImageText()
{
this.InitializeComponent();
}
private void btnTextBodyWrap_Click_1(object sender, RoutedEventArgs e)
{
IToastImageAndText01 templateContent = ToastContentFactory.CreateToastImageAndText01();
templateContent.TextBodyWrap.Text = "我是通知正文,可换行,最多三行。我是通知正文,可换行,最多三行。我是通知正文,可换行,最多三行。";
templateContent.Image.Src = "Assets/Logo.png"; // 用程序包内文件作通知图片
templateContent.Image.Alt = "altText";
IToastNotificationContent toastContent = templateContent;
ToastNotification toast = toastContent.CreateNotification();
ToastNotificationManager.CreateToastNotifier().Show(toast);
lblMsg.Text = toastContent.GetContent();
}
private void bntTextHeadingTextBodyWrap_Click_1(object sender, RoutedEventArgs e)
{
IToastImageAndText02 templateContent = ToastContentFactory.CreateToastImageAndText02();
templateContent.TextHeading.Text = "我是通知标题,不可以换行。我是通知标题,不可以换行。";
templateContent.TextBodyWrap.Text = "我是通知正文,可换行,最多两行。我是通知正文,可换行,最多两行。";
templateContent.Image.Src = "ms-appx:///Assets/Logo.png"; // 用程序包内文件作通知图片
templateContent.Image.Alt = "altText";
IToastNotificationContent toastContent = templateContent;
ToastNotification toast = toastContent.CreateNotification();
ToastNotificationManager.CreateToastNotifier().Show(toast);
lblMsg.Text = toastContent.GetContent();
}
private void bntTextHeadingWrapTextBody_Click_1(object sender, RoutedEventArgs e)
{
IToastImageAndText03 templateContent = ToastContentFactory.CreateToastImageAndText03();
templateContent.TextHeadingWrap.Text = "我是通知标题,可换行,最多两行。我是通知标题,可换行,最多两行。";
templateContent.TextBody.Text = "我是通知正文,不可以换行。我是通知正文,不可以换行。";
templateContent.Image.Src = "ms-appdata:///local/Logo.png"; // 用 Application Data 内文件作通知图片(注:仅支持 local 中的图片)
templateContent.Image.Alt = "altText";
IToastNotificationContent toastContent = templateContent;
ToastNotification toast = toastContent.CreateNotification();
ToastNotificationManager.CreateToastNotifier().Show(toast);
lblMsg.Text = toastContent.GetContent();
}
private void bntTextHeadingTextBody_Click_1(object sender, RoutedEventArgs e)
{
IToastImageAndText04 templateContent = ToastContentFactory.CreateToastImageAndText04();
templateContent.TextHeading.Text = "我是通知标题,不可以换行。我是通知标题,不可以换行。";
templateContent.TextBody1.Text = "我是通知正文1,不可以换行。我是通知正文1,不可以换行。";
templateContent.TextBody2.Text = "我是通知正文2,不可以换行。我是通知正文2,不可以换行。";
templateContent.Image.Src = "http://pic.iyunv.com/avatar/a14540.jpg?id=24173245"; // 用远程文件作通知图片
templateContent.Image.Alt = "altText";
IToastNotificationContent toastContent = templateContent;
ToastNotification toast = toastContent.CreateNotification();
ToastNotificationManager.CreateToastNotifier().Show(toast);
lblMsg.Text = toastContent.GetContent();
}
}
}
  
3、演示 Toast 的提示音
Notification/Toast/ToastWithSound.xaml























  Notification/Toast/ToastWithSound.xaml.cs



/*
* 演示 Toast 的提示音
*
* 目前支持的 Toast 提示音共有以下几种:
* Default, Mail, SMS, IM, Reminder, LoopingCall, LoopingCall2, LoopingAlarm, LoopingAlarm2, Silent
*/
using NotificationsExtensions.ToastContent;
using System;
using Windows.UI.Notifications;
using Windows.UI.Xaml.Controls;
namespace XamlDemo.Notification.Toast
{
public sealed partial class ToastWithSound : Page
{
public ToastWithSound()
{
this.InitializeComponent();
}
private void listBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
string audioType = (listBox.SelectedItem as ListBoxItem).Content.ToString();
IToastText02 toastContent = ToastContentFactory.CreateToastText02();
toastContent.TextHeading.Text = "Sound:";
toastContent.TextBodyWrap.Text = audioType;
toastContent.Audio.Content = (ToastAudioContent)Enum.Parse(typeof(ToastAudioContent), audioType);
/*
* LoopingCall, LoopingCall2, LoopingAlarm, LoopingAlarm2 这 4 种提示音仅针对长时通知,且需指定为循环提示音
*/
if (audioType == "LoopingCall" || audioType == "LoopingCall2" || audioType == "LoopingAlarm" || audioType == "LoopingAlarm2")
{
toastContent.Duration = ToastDuration.Long;
toastContent.Audio.Loop = true;
}
ToastNotification toast = toastContent.CreateNotification();
ToastNotificationManager.CreateToastNotifier().Show(toast);
lblMsg.Text = toastContent.GetContent();
}
}
}
  
4、演示如何按计划显示 Toast 通知
Notification/Toast/ScheduledToast.xaml






















  Notification/Toast/ScheduledToast.xaml.cs



/*
* 演示如何按计划显示 Toast 通知
*
* ScheduledToastNotification - 按计划显示 Toast 通知
*     Content - Toast 的内容,XmlDocument 类型的数据,只读,其需要在构造函数中指定
*     DeliveryTime - 显示 Toast 通知的时间,只读,其需要在构造函数中指定
*     SnoozeInterval - 循环显示 Toast 通知的间隔时长(60 秒 - 60 分之间),只读,其需要在构造函数中指定
*     MaximumSnoozeCount - 循环的最大次数(1 - 5 次)
*     Id - ScheduledToastNotification 的标识
*     
* ToastNotifier - Toast 通知器
*     AddToSchedule() - 将指定的 ScheduledToastNotification 添加到计划列表
*     RemoveFromSchedule() - 从计划列表中移除指定的 ScheduledToastNotification
*     GetScheduledToastNotifications() - 获取当前 app 的全部 ScheduledToastNotification 集合
*/
using NotificationsExtensions.ToastContent;
using System;
using System.Collections.Generic;
using Windows.UI.Notifications;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace XamlDemo.Notification.Toast
{
public sealed partial class ScheduledToast : Page
{
public ScheduledToast()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
ShowScheduledToasts();
}
// 添加指定的 ScheduledToastNotification 到计划列表中
private void btnScheduledToast_Click_1(object sender, RoutedEventArgs e)
{
IToastText02 toastContent = ToastContentFactory.CreateToastText02();
toastContent.TextHeading.Text = "ScheduledToastNotification Demo";
toastContent.TextBodyWrap.Text = "received: " + DateTime.Now.ToString("hh:mm:ss");
// 3 秒后显示 Toast,然后每隔 60 秒再显示一次 Toast(循环显示 5 次)
ScheduledToastNotification toast = new ScheduledToastNotification(toastContent.GetXml(), DateTime.Now.AddSeconds(3), TimeSpan.FromSeconds(60), 5);
string toastId = new Random().Next(1000, 10000).ToString();
toast.Id = toastId;
// 将指定的 ScheduledToastNotification 添加进计划列表
ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier();
toastNotifier.AddToSchedule(toast);
ShowScheduledToasts();
}
// 显示当前 app 的全部 ScheduledToastNotification 列表
private void ShowScheduledToasts()
{
List dataSource = new List();
// 获取当前 app 计划列表中的全部 ScheduledToastNotification 对象列表
ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier();
IReadOnlyList scheduledToasts = toastNotifier.GetScheduledToastNotifications();
int toastCount = scheduledToasts.Count;
for (int i = 0; i < toastCount; i++)
{
ScheduledToastNotification toast = scheduledToasts;
dataSource.Add(new MyScheduledToast()
{
ToastId = toast.Id,
Text = toast.Content.GetElementsByTagName("text")[0].InnerText
});
}
listBox.ItemsSource = dataSource;
}
// 根据 ToastId 删除指定的 ScheduledToastNotification
private void btnRemove_Click_1(object sender, RoutedEventArgs e)
{
string toastId = (string)(sender as FrameworkElement).Tag;
// 获取当前 app 计划列表中的全部 ScheduledToastNotification 对象列表
ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier();
IReadOnlyList scheduledToasts = toastNotifier.GetScheduledToastNotifications();
int toastLength = scheduledToasts.Count;
for (int i = 0; i < toastLength; i++)
{
if (scheduledToasts.Id == toastId)
{
// 从计划列表中移除指定的 ScheduledToastNotification 对象
                    toastNotifier.RemoveFromSchedule(scheduledToasts);
ShowScheduledToasts();
break;
}
}
}
class MyScheduledToast
{
public string ToastId { get; set; }
public string Text { get; set; }
}
}
}
  
OK
[源码下载]

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-69316-1-1.html 上篇帖子: 重新想象 Windows 8 Store Apps (57) 下篇帖子: Attached Command for Windows 8 Metro Style in C#
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表