您的位置:首页 > 移动开发

背水一战 Windows 10 (112) - 通知(Badge): application 的 badge 通知, secondary 的 badge 通知, 轮询服务端以更新 badge 通知

2018-06-21 08:34 405 查看

[源码下载]


背水一战 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(30);
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(30);
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(30);
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(30);
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(45), PeriodicUpdateRecurrence.HalfHour);

// Badge 通知的数据源示例请参见 WebApi/Controllers/BadgeContentController.cs
}
}
}



OK
[源码下载]

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐