背水一战 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 ...
随机推荐
- Linux 对信号的总结
Linux信号_总结 对信号本质的理解: 类似于中断,区别在于中断是由硬件产生的,而信号是由软件实现的. 信号的来源: 触发硬件(触发键盘,或是硬件故障):软件信号函数kill .alarm.seti ...
- Checkbox的只读设置
readonly和disabled属性均不生效.可按如下方式处理,记得引入jquery.js文件 <input type="checkbox" name="chk& ...
- 分析abex-crackme#1
1.分析环境2.运行程序,了解大致的运行过程3.运行Ollydbg调试程序3.1.分析结果简述4.破解4.1.方法一4.2.方法二5.运行结果6.与书中不同之处 1.分析环境 操作系统:Win10 1 ...
- tomcat设置开机自动启动
windows下: 1 命令cmd 进入 磁盘: 回车 2 tomcat\bin目录 回车 3 service.bat install (注:tomcat一定要有service.bat ...
- Java 中12个原子操作类
从JDK1.5 开始提供了 java.util.concurrent.atomic 包,该包提供了一种用法简单.性能高效.线程安全的更新一个变量的方法 原子更新基本类型类 AtomicBoolean: ...
- 序列化还是JSON存储对象?
2018-11-10 先对比一下存诸空间大小:https://pan.baidu.com/s/1I3K8ARfl4x8pC__B-T_vbA 输出的结果是 json: 序列化: 序列化Map: 用JS ...
- bower简明入门教程
什么是bower: Bower是一个客户端技术的软件包管理器,它可用于搜索.安装和卸载如JavaScript.HTML.CSS之类的网络资源.其他一些建立在Bower基础之上的开发工具,如YeoMan ...
- Springboot & Mybatis 构建restful 服务
Springboot & Mybatis 构建restful 服务一 1 前置条件 jdk测试:java -version maven测试:命令行之行mvn -v eclipse及maven插 ...
- flag:用心学习的第一天
目标是:加油学习,尽早改变世界
- python的文件读写笔记
读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘, ...