背水一战 Windows 10 (112) - 通知(Badge): application 的 badge 通知, secondary 的 badge 通知, 轮询服务端以更新 badge 通知
作者:webabcd
介绍
背水一战 Windows 10 之 通知(Badge)
- application 的 badge 通知
- secondary 的 badge 通知
- 轮询服务端以更新 badge 通知
示例
1、本例用于演示 application 的 badge 通知
Notification/Badge/ApplicationBadge.xaml
<Page
x:Class="Windows10.Notification.Badge.ApplicationBadge"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Notification.Badge"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <Button Name="btnUpdateBadgeWidthNumber" Content="以数字的方式更新 Application Badge" Click="btnUpdateBadgeWidthNumber_Click" Margin="5" /> <StackPanel Orientation="Horizontal" Margin="5">
<ComboBox Name="cmbBadgeValue">
<ComboBoxItem Content="activity" IsSelected="True" />
<ComboBoxItem Content="alarm" />
<ComboBoxItem Content="alert" />
<ComboBoxItem Content="attention" />
<ComboBoxItem Content="available" />
<ComboBoxItem Content="away" />
<ComboBoxItem Content="busy" />
<ComboBoxItem Content="error" />
<ComboBoxItem Content="newMessage" />
<ComboBoxItem Content="paused" />
<ComboBoxItem Content="playing" />
<ComboBoxItem Content="unavailable" />
</ComboBox>
<Button Name="btnUpdateBadgeWidthIcon" Content="以图标的方式更新 Application Badge" Click="btnUpdateBadgeWidthIcon_Click" Margin="5 0 0 0" />
</StackPanel> <Button Name="btnClearBadge" Content="清除 Application Badge" Click="btnClearBadge_Click" Margin="5" /> </StackPanel>
</Grid>
</Page>
Notification/Badge/ApplicationBadge.xaml.cs
/*
* 本例用于演示 application 的 badge 通知
* application 的 badge 通知会显示在 application tile 上和 app 的任务栏的图标上
*
* BadgeNotification - Badge 通知
* Content - Badge 的内容,XmlDocument 类型的数据,只读,其需要在构造函数中指定
* ExpirationTime - Badge 通知的过期时间,超过这个时间就会清除这个 Badge(application tile 的 badge 会被清除,而 app 的任务栏的图标上 的 badge 不会被清除)
*
* BadgeUpdateManager - Badge 更新管理器
* CreateBadgeUpdaterForApplication() - 创建一个 Application 的 Badge 更新器,返回 BadgeUpdater 类型的数据
*
* BadgeUpdater - Badge 更新器
* Update() - 更新指定的 BadgeNotification
* Clear() - 清除 BadgeNotification
*
*
* 注:本例是通过 xml 来构造 badge 的,另外也可以通过 NuGet 的 Microsoft.Toolkit.Uwp.Notifications 来构造 badge(其用 c# 对 xml 做了封装)
*/ using System;
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows10.Notification.Badge
{
public sealed partial class ApplicationBadge : Page
{
public ApplicationBadge()
{
this.InitializeComponent();
} // 以数字的方式更新 Application Badge 通知
private void btnUpdateBadgeWidthNumber_Click(object sender, RoutedEventArgs e)
{
// 用于描述 badge 通知的 xml 字符串(数字在 1 - 99 之间,如果大于 99 则会显示 99+ ,如果是 0 则会移除 badge,如果小于 0 则无效)
string badgeXml = "<badge value='6'/>"; // 将 xml 字符串转换为 Windows.Data.Xml.Dom.XmlDocument 对象
XmlDocument badgeDoc = new XmlDocument();
badgeDoc.LoadXml(badgeXml);
// 获取此 badge 的 xml
// lblMsg.Text = badgeXml.GetXml(); // 实例化 BadgeNotification 对象
BadgeNotification badgeNotification = new BadgeNotification(badgeDoc);
DateTimeOffset expirationTime = DateTimeOffset.UtcNow.AddSeconds();
badgeNotification.ExpirationTime = expirationTime; // 30 秒后清除这个 badge // 将指定的 BadgeNotification 对象更新到 application
BadgeUpdater badgeUpdater = BadgeUpdateManager.CreateBadgeUpdaterForApplication();
badgeUpdater.Update(badgeNotification);
} // 以图标的方式更新 Application Badge 通知
private void btnUpdateBadgeWidthIcon_Click(object sender, RoutedEventArgs e)
{
// 用于描述 badge 通知的 xml 字符串
string badgeXml = $"<badge value='{((ComboBoxItem)cmbBadgeValue.SelectedItem).Content}'/>"; // 将 xml 字符串转换为 Windows.Data.Xml.Dom.XmlDocument 对象
XmlDocument badgeDoc = new XmlDocument();
badgeDoc.LoadXml(badgeXml);
// 获取此 badge 的 xml
// lblMsg.Text = badgeXml.GetXml(); // 实例化 BadgeNotification 对象
BadgeNotification badgeNotification = new BadgeNotification(badgeDoc);
DateTimeOffset expirationTime = DateTimeOffset.UtcNow.AddSeconds();
badgeNotification.ExpirationTime = expirationTime; // 30 秒后清除这个 badge // 将指定的 BadgeNotification 对象更新到 application
BadgeUpdater badgeUpdater = BadgeUpdateManager.CreateBadgeUpdaterForApplication();
badgeUpdater.Update(badgeNotification);
} // 清除 Application Badge 通知
private void btnClearBadge_Click(object sender, RoutedEventArgs e)
{
BadgeUpdater badgeUpdater = BadgeUpdateManager.CreateBadgeUpdaterForApplication();
badgeUpdater.Clear();
}
}
}
2、本例用于演示 secondary tile 的 badge 通知
Notification/Badge/SecondaryTileBadge.xaml
<Page
x:Class="Windows10.Notification.Badge.SecondaryTileBadge"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Notification.Badge"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5" /> <Button Name="btnUpdateBadgeWidthNumber" Content="以数字的方式更新指定的 Secondary Tile 的 Badge" Click="btnUpdateBadgeWidthNumber_Click" Margin="5" /> <StackPanel Orientation="Horizontal" Margin="5">
<ComboBox Name="cmbBadgeValue">
<ComboBoxItem Content="activity" IsSelected="True" />
<ComboBoxItem Content="alarm" />
<ComboBoxItem Content="alert" />
<ComboBoxItem Content="attention" />
<ComboBoxItem Content="available" />
<ComboBoxItem Content="away" />
<ComboBoxItem Content="busy" />
<ComboBoxItem Content="error" />
<ComboBoxItem Content="newMessage" />
<ComboBoxItem Content="paused" />
<ComboBoxItem Content="playing" />
<ComboBoxItem Content="unavailable" />
</ComboBox>
<Button Name="btnUpdateBadgeWidthIcon" Content="以图标的方式更新指定的 Secondary Tile 的 Badge" Click="btnUpdateBadgeWidthIcon_Click" Margin="5 0 0 0" />
</StackPanel> <Button Name="btnClearBadge" Content="清除指定的 Secondary Tile 的 Badge" Click="btnClearBadge_Click" Margin="5" /> </StackPanel>
</Grid>
</Page>
Notification/Badge/SecondaryTileBadge.xaml.cs
/*
* 本例用于演示 secondary tile 的 badge 通知
* secondary tile 的 badge 通知会显示在 secondary tile 上和 app 的任务栏的图标上
*
* BadgeNotification - Badge 通知
* Content - Badge 的内容,XmlDocument 类型的数据,只读,其需要在构造函数中指定
* ExpirationTime - Badge 通知的过期时间,超过这个时间就会清除这个 Badge
*
* BadgeUpdateManager - Badge 更新管理器
* CreateBadgeUpdaterForSecondaryTile(string tileId) - 为指定的 secondary tile 创建一个 Badge 更新器,返回 BadgeUpdater 类型的数据
*
* BadgeUpdater - Badge 更新器
* Update() - 更新指定的 BadgeNotification
* Clear() - 清除 BadgeNotification
*
*
* 注:本例是通过 xml 来构造 badge 的,另外也可以通过 NuGet 的 Microsoft.Toolkit.Uwp.Notifications 来构造 badge(其用 c# 对 xml 做了封装)
*/ using System;
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
using Windows.UI.StartScreen;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; namespace Windows10.Notification.Badge
{
public sealed partial class SecondaryTileBadge : Page
{
private const string TILEID = "tile_badge"; public SecondaryTileBadge()
{
this.InitializeComponent();
} // 在开始屏幕固定一个 secondary tile 磁贴
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e); Uri square150x150Logo = new Uri("ms-appx:///Assets/Square150x150Logo.png");
Uri wide310x150Logo = new Uri("ms-appx:///Assets/Wide310x150Logo.png");
Uri square310x310Logo = new Uri("ms-appx:///Assets/Square310x310Logo.png");
SecondaryTile secondaryTile = new SecondaryTile(TILEID, "name", "arguments", square150x150Logo, TileSize.Wide310x150);
secondaryTile.VisualElements.Wide310x150Logo = wide310x150Logo;
secondaryTile.VisualElements.Square310x310Logo = square310x310Logo; try
{
bool isPinned = await secondaryTile.RequestCreateAsync();
lblMsg.Text = isPinned ? "固定成功" : "固定失败";
}
catch (Exception ex)
{
lblMsg.Text = "固定失败: " + ex.ToString();
}
} // 以数字的方式更新指定的 Secondary Tile 的 Badge 通知
private void btnUpdateBadgeWidthNumber_Click(object sender, RoutedEventArgs e)
{
// 用于描述 badge 通知的 xml 字符串(数字在 1 - 99 之间,如果大于 99 则会显示 99+ ,如果是 0 则会移除 badge,如果小于 0 则无效)
string badgeXml = "<badge value='6'/>"; // 将 xml 字符串转换为 Windows.Data.Xml.Dom.XmlDocument 对象
XmlDocument badgeDoc = new XmlDocument();
badgeDoc.LoadXml(badgeXml);
// 获取此 badge 的 xml
// lblMsg.Text = badgeXml.GetXml(); // 实例化 BadgeNotification 对象
BadgeNotification badgeNotification = new BadgeNotification(badgeDoc);
DateTimeOffset expirationTime = DateTimeOffset.UtcNow.AddSeconds();
badgeNotification.ExpirationTime = expirationTime; // 30 秒后清除这个 badge // 将指定的 BadgeNotification 对象更新到指定的 secondary tile 磁贴
BadgeUpdater badgeUpdater = BadgeUpdateManager.CreateBadgeUpdaterForSecondaryTile(TILEID);
badgeUpdater.Update(badgeNotification);
} // 以图标的方式更新指定的 Secondary Tile 的 Badge 通知
private void btnUpdateBadgeWidthIcon_Click(object sender, RoutedEventArgs e)
{
// 用于描述 badge 通知的 xml 字符串
string badgeXml = $"<badge value='{((ComboBoxItem)cmbBadgeValue.SelectedItem).Content}'/>"; // 将 xml 字符串转换为 Windows.Data.Xml.Dom.XmlDocument 对象
XmlDocument badgeDoc = new XmlDocument();
badgeDoc.LoadXml(badgeXml);
// 获取此 badge 的 xml
// lblMsg.Text = badgeXml.GetXml(); // 实例化 BadgeNotification 对象
BadgeNotification badgeNotification = new BadgeNotification(badgeDoc);
DateTimeOffset expirationTime = DateTimeOffset.UtcNow.AddSeconds();
badgeNotification.ExpirationTime = expirationTime; // 30 秒后清除这个 badge // 将指定的 BadgeNotification 对象更新到指定的 secondary tile 磁贴
BadgeUpdater badgeUpdater = BadgeUpdateManager.CreateBadgeUpdaterForSecondaryTile(TILEID);
badgeUpdater.Update(badgeNotification);
} // 清除指定的 Secondary Tile 的 Badge 通知
private void btnClearBadge_Click(object sender, RoutedEventArgs e)
{
BadgeUpdater badgeUpdater = BadgeUpdateManager.CreateBadgeUpdaterForSecondaryTile(TILEID);
badgeUpdater.Clear();
}
}
}
3、演示如何轮询服务端以更新 badge 通知
Notification/Badge/Periodic.xaml
<Page
x:Class="Windows10.Notification.Badge.Periodic"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Notification.Badge"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5" /> <Button Name="btnStartPeriodicUpdate" Content="启动一个“轮询服务端以更新 badge 通知”的任务" Click="btnStartPeriodicUpdate_Click" Margin="5" /> </StackPanel>
</Grid>
</Page>
Notification/Badge/Periodic.xaml.cs
/*
* 演示如何轮询服务端以更新 badge 通知
*
* BadgeUpdater - Badge 更新器
* StartPeriodicUpdate(Uri badgeContent, PeriodicUpdateRecurrence requestedInterval) - 启动一个“轮询服务端以更新 badge 通知”的任务
* StartPeriodicUpdate(Uri badgeContent, DateTimeOffset startTime, PeriodicUpdateRecurrence requestedInterval) - 启动一个“轮询服务端以更新 badge 通知”的任务
* badgeContent - Badge 通知的内容(xml 格式数据)的 uri 地址(指定多个则会循环显示)
* startTime - 可以指定启动此任务的时间
* 指定此值时的逻辑为:首先会立刻请求服务端获取数据,然后在到达 startTime 指定的时间点后再次获取数据,最后再每次按 requestedInterval 指定的间隔轮询服务端
* requestedInterval - 轮询服务端的周期(PeriodicUpdateRecurrence 枚举)
* HalfHour, Hour, SixHours, TwelveHours, Daily
* StopPeriodicUpdate() - 停止“轮询服务端以更新 badge 通知”的任务
*
*
* 注:服务端代码请参见 WebApi/Controllers/BadgeContentController.cs(有指定 X-WNS-Expires 标头和 X-WNS-Tag 标头的示例)
*/ using System;
using Windows.UI.Notifications;
using Windows.UI.StartScreen;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; namespace Windows10.Notification.Badge
{
public sealed partial class Periodic : Page
{
private const string TILEID = "tile_badge_periodic"; public Periodic()
{
this.InitializeComponent();
} // 在开始屏幕固定一个 secondary tile 磁贴
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e); Uri square150x150Logo = new Uri("ms-appx:///Assets/Square150x150Logo.png");
Uri wide310x150Logo = new Uri("ms-appx:///Assets/Wide310x150Logo.png");
Uri square310x310Logo = new Uri("ms-appx:///Assets/Square310x310Logo.png");
SecondaryTile secondaryTile = new SecondaryTile(TILEID, "name", "arguments", square150x150Logo, TileSize.Wide310x150);
secondaryTile.VisualElements.Wide310x150Logo = wide310x150Logo;
secondaryTile.VisualElements.Square310x310Logo = square310x310Logo; try
{
bool isPinned = await secondaryTile.RequestCreateAsync();
lblMsg.Text = isPinned ? "固定成功" : "固定失败";
}
catch (Exception ex)
{
lblMsg.Text = "固定失败: " + ex.ToString();
}
} // 启动一个“轮询服务端以更新 badge 通知”的任务
private void btnStartPeriodicUpdate_Click(object sender, RoutedEventArgs e)
{
// 启动一个循环更新 Badge 通知的任务,并指定 Badge 通知的数据源和轮询周期
BadgeUpdater badgeUpdater = BadgeUpdateManager.CreateBadgeUpdaterForSecondaryTile(TILEID); // 马上请求服务端获取数据,然后 45 分钟之后再次获取数据,最后再每半个小时获取一次数据
badgeUpdater.StartPeriodicUpdate(new Uri("http://localhost:44914/api/BadgeContent", UriKind.Absolute), DateTimeOffset.UtcNow.AddMinutes(), PeriodicUpdateRecurrence.HalfHour); // Badge 通知的数据源示例请参见 WebApi/Controllers/BadgeContentController.cs
}
}
}
OK
[源码下载]
背水一战 Windows 10 (112) - 通知(Badge): application 的 badge 通知, secondary 的 badge 通知, 轮询服务端以更新 badge 通知的更多相关文章
- 背水一战 Windows 10 (109) - 通知(Tile): 按计划显示 tile 通知, 轮询服务端以更新 tile 通知
[源码下载] 背水一战 Windows 10 (109) - 通知(Tile): 按计划显示 tile 通知, 轮询服务端以更新 tile 通知 作者:webabcd 介绍背水一战 Windows 1 ...
- 背水一战 Windows 10 (118) - 后台任务: 后台下载任务(任务分组,并行或串行执行,组完成后通知)
[源码下载] 背水一战 Windows 10 (118) - 后台任务: 后台下载任务(任务分组,并行或串行执行,组完成后通知) 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 ...
- 背水一战 Windows 10 (113) - 锁屏: 将 Application 的 Badge 通知和 Tile 通知发送到锁屏, 将 secondary tile 的 Badge 通知和 Tile 通知发送到锁屏
[源码下载] 背水一战 Windows 10 (113) - 锁屏: 将 Application 的 Badge 通知和 Tile 通知发送到锁屏, 将 secondary tile 的 Badge ...
- 背水一战 Windows 10 (108) - 通知(Tile): application tile 基础, secondary tile 基础
[源码下载] 背水一战 Windows 10 (108) - 通知(Tile): application tile 基础, secondary tile 基础 作者:webabcd 介绍背水一战 Wi ...
- 背水一战 Windows 10 (121) - 后台任务: 推送通知
[源码下载] 背水一战 Windows 10 (121) - 后台任务: 推送通知 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 推送通知 示例演示如何接收推送通知/WebA ...
- 背水一战 Windows 10 (111) - 通知(Tile): secondary tile 模板之图片, secondary tile 模板之分组
[源码下载] 背水一战 Windows 10 (111) - 通知(Tile): secondary tile 模板之图片, secondary tile 模板之分组 作者:webabcd 介绍背水一 ...
- 背水一战 Windows 10 (110) - 通知(Tile): secondary tile 模板之基础, secondary tile 模板之文本
[源码下载] 背水一战 Windows 10 (110) - 通知(Tile): secondary tile 模板之基础, secondary tile 模板之文本 作者:webabcd 介绍背水一 ...
- 背水一战 Windows 10 (107) - 通知(Toast): 提示音, 特定场景
[源码下载] 背水一战 Windows 10 (107) - 通知(Toast): 提示音, 特定场景 作者:webabcd 介绍背水一战 Windows 10 之 通知(Toast) 提示音 特定场 ...
- 背水一战 Windows 10 (106) - 通知(Toast): 通过 toast 打开协议, 通过 toast 选择在指定的时间之后延迟提醒或者取消延迟提醒
[源码下载] 背水一战 Windows 10 (106) - 通知(Toast): 通过 toast 打开协议, 通过 toast 选择在指定的时间之后延迟提醒或者取消延迟提醒 作者:webabcd ...
随机推荐
- [leetcode]432. All O`one Data Structure全O(1)数据结构
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- mysql启动服务
mysql.server start 启动mysql服务mysql.server stop 停止mysql服务 mysql密码:123456Az_
- mac 删除文件不经过废纸篓解决办法
mac 删除文件不经过废纸篓,提示“此项目将被立刻删除,您不能撤销此操作.”,解决办法. 终端机运行两个命令: rm -R ~/.Trash killall Finder 退出终端机. ------- ...
- JS学习记录------JS基本指令
对未来的恐慌,和想成为一名自由开发的梦想.让我觉得应该点亮一个新的技能:WEB前端开发. 重新学习JS以及jQuery,让我在日常code的过程中可以更得心应手,毕竟,我爱代码. 这篇文章主要记录的内 ...
- 使用QML绘制界面
1 使用QML设计登录界面 https://www.cnblogs.com/bhlsheji/p/5324871.html 2 使用QML实现下拉列表框 https://blog.csdn.net/ ...
- 第一次面试经历(hr面)
经过介绍,我有幸去到一家国际背景的广告公司面试前端开发实习生.收到的邮件是复试通知,看来我已经跳过了第一轮面试. 来到hr请我进了一个小间坐下里填求职书,里面有各种个人信息,有兴趣爱好,有工作经历,以 ...
- win10无法访问服务器上的共享文件夹怎么设置,提示:你不能访问此共享文件夹,因为你组织的安全策略阻止未经身份验证的来宾访问
此问题需要修改Win10 网络策略 按window+R键输入gpedit.msc 来启动本地组策略编辑器. 依次找到“计算机配置-管理模板-网络-Lanman工作站”这个节点,在右侧内容区可以看到“启 ...
- Mybaits API docs
http://www.mybatis.org/mybatis-3/apidocs/index.html
- 如何判断Linux是32位还是64位
getconf LONG_BIT
- PHP 使用数字作为SESSION的Key,一刷新页面session丢失,Session消失,无法存储
PHP 使用数字作为SESSION的Key,一刷新页面session丢失,Session消失,无法存储 项目中有用到md5截取做session key值的,有些md5截取出来的部分是纯数字的,导致部分 ...