背水一战 Windows 10 (109) - 通知(Tile): 按计划显示 tile 通知, 轮询服务端以更新 tile 通知
作者:webabcd
介绍
背水一战 Windows 10 之 通知(Tile)
- 按计划显示 tile 通知
- 轮询服务端以更新 tile 通知
示例
1、演示如何按计划显示 tile 通知(在指定的时间显示指定的 tile 通知,此特性在 application tile 和 secondary tile 中均支持)
Notification/Tile/Schedule.xaml
<Page
x:Class="Windows10.Notification.Tile.Schedule"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Notification.Tile"
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" /> <!--显示指定 secondary tile 的全部 ScheduledTileNotification 列表-->
<ListBox Name="listBox" Width="800" Height="400" Margin="5" HorizontalAlignment="Left">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Id}" VerticalAlignment="Center" />
<TextBlock Text="{Binding Tag}" Margin="15 0 0 0" VerticalAlignment="Center" />
<HyperlinkButton Name="btnRemoveScheduledTile" Content="删除此 ScheduledTileNotification" Tag="{Binding Id}" Margin="15 0 0 0" Click="btnRemoveScheduledTile_Click" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox> <Button Name="btnAddScheduledTile" Content="添加指定的 ScheduledTileNotification 到指定的 secondary tile 的计划列表中" Click="btnAddScheduledTile_Click" Margin="5" /> </StackPanel>
</Grid>
</Page>
Notification/Tile/Schedule.xaml.cs
/*
* 演示如何按计划显示 tile 通知(在指定的时间显示指定的 tile 通知,此特性在 application tile 和 secondary tile 中均支持)
*
* ScheduledTileNotification - 按计划显示的 Tile 通知
* Content - Tile 的内容,XmlDocument 类型的数据,只读,其需要在构造函数中指定
* DeliveryTime - 显示 Tile 通知的时间,只读,其需要在构造函数中指定
* ExpirationTime - Tile 通知的过期时间,超过这个时间就会清除这个 Tile
* Id - ScheduledTileNotification 的标识
* Tag - 在启用 tile 的队列功能时,如果 tile 的 Tag 相同则新的内容会更新旧的内容(Tag 值的前 16 个字符相同则认为是相同的 Tag)
* 不指定的 Tag 的话则认为 Tag 都是不同的
*
* TileUpdater - 磁贴的 Tile 更新器
* AddToSchedule() - 将指定的 ScheduledTileNotification 对象添加到计划列表
* RemoveFromSchedule() - 从计划列表中移除指定的 ScheduledTileNotification 对象
* GetScheduledTileNotifications() - 获取全部 ScheduledTileNotification 对象列表
*/ using System;
using System.Collections.Generic;
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.Tile
{
public sealed partial class Schedule : Page
{
private const string TILEID = "tile_schedule"; public Schedule()
{
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();
} ShowScheduledTiles();
} // 添加指定的 ScheduledTileNotification 到指定的 secondary tile 的计划列表中
private void btnAddScheduledTile_Click(object sender, RoutedEventArgs e)
{
string tileXml = $@"
<tile>
<visual>
<binding template='TileSmall'>
<text>Small(小){DateTime.Now.ToString("HH:mm:ss")}</text>
</binding>
<binding template='TileMedium'>
<text>Medium(中){DateTime.Now.ToString("HH:mm:ss")}</text>
</binding>
<binding template='TileWide'>
<text>Wide(宽){DateTime.Now.ToString("HH:mm:ss")}</text>
</binding>
<binding template='TileLarge'>
<text>Large(大){DateTime.Now.ToString("HH:mm:ss")}</text>
</binding>
</visual>
</tile>"; XmlDocument tileDoc = new XmlDocument();
tileDoc.LoadXml(tileXml); // 实例化 ScheduledTileNotification 对象(15 秒后显示此 Tile 通知)
DateTime dt = DateTime.Now.AddSeconds();
ScheduledTileNotification tileNotification = new ScheduledTileNotification(tileDoc, dt); tileNotification.Id = new Random().Next(, ).ToString(); ;
tileNotification.Tag = $"在 {dt.ToString("HH:mm:ss")} 时显示此 tile 通知"; // 将指定的 ScheduledTileNotification 对象添加进指定的 secondary tile 的计划列表
TileUpdater tileUpdater = TileUpdateManager.CreateTileUpdaterForSecondaryTile(TILEID);
tileUpdater.AddToSchedule(tileNotification);
tileUpdater.EnableNotificationQueue(true); // 启用 tile 的队列功能(最多可容纳 5 个 tile) ShowScheduledTiles();
} // 删除指定 secondary tile 的指定 ScheduledTileNotification 对象
private void btnRemoveScheduledTile_Click(object sender, RoutedEventArgs e)
{
string notificationId = (string)(sender as FrameworkElement).Tag; // 获取指定 secondary tile 的全部 ScheduledTileNotification 对象列表
TileUpdater tileUpdater = TileUpdateManager.CreateTileUpdaterForSecondaryTile(TILEID);
IReadOnlyList<ScheduledTileNotification> notifications = tileUpdater.GetScheduledTileNotifications(); int notificationCount = notifications.Count;
for (int i = ; i < notificationCount; i++)
{
if (notifications[i].Id == notificationId)
{
// 从计划列表中移除指定的 ScheduledTileNotification 对象
tileUpdater.RemoveFromSchedule(notifications[i]);
break;
}
} ShowScheduledTiles();
} // 显示指定 secondary tile 的全部 ScheduledTileNotification 列表
private void ShowScheduledTiles()
{
// 获取指定 secondary tile 的全部 ScheduledTileNotification 对象列表
TileUpdater tileUpdater = TileUpdateManager.CreateTileUpdaterForSecondaryTile(TILEID);
IReadOnlyList<ScheduledTileNotification> notifications = tileUpdater.GetScheduledTileNotifications(); listBox.ItemsSource = notifications;
}
}
}
2、演示如何轮询服务端以更新 tile 通知
Notification/Tile/Periodic.xaml
<Page
x:Class="Windows10.Notification.Tile.Periodic"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Notification.Tile"
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="启动一个“轮询服务端获取数据,然后更新 Tile 通知”的任务" Click="btnStartPeriodicUpdate_Click" Margin="5" /> </StackPanel>
</Grid>
</Page>
Notification/Tile/Periodic.xaml.cs
/*
* 演示如何轮询服务端以更新 tile 通知
*
* TileUpdater - 磁贴的 Tile 更新器
* StartPeriodicUpdate(Uri tileContent, DateTimeOffset startTime, PeriodicUpdateRecurrence requestedInterval) - 启动一个“轮询服务端获取数据,然后更新 Tile 通知”的任务
* StartPeriodicUpdateBatch(IEnumerable<Uri> tileContents, DateTimeOffset startTime, PeriodicUpdateRecurrence requestedInterval) - 启动一个“轮询服务端获取数据,然后更新 Tile 通知”的任务
* tileContent - Tile 通知的内容(xml 格式数据)的 uri 地址(通过 StartPeriodicUpdateBatch 方法指定多个则会循环显示)
* startTime - 可以指定启动此任务的时间
* 指定此值时的逻辑为:首先会立刻请求服务端获取数据,然后在到达 startTime 指定的时间点后再次获取数据,最后再每次按 requestedInterval 指定的间隔轮询服务端
* requestedInterval - 轮询服务端的周期(PeriodicUpdateRecurrence 枚举)
* HalfHour, Hour, SixHours, TwelveHours, Daily
* StopPeriodicUpdate() - 停止“轮询服务端获取数据,然后更新 Tile 通知”的任务
*
*
* 注:服务端代码请参见 WebApi/Controllers/TileContentController.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.Tile
{
public sealed partial class Periodic : Page
{
private const string TILEID = "tile_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();
}
} // 启动一个“轮询服务端获取数据,然后更新 Tile 通知”的任务
private void btnStartPeriodicUpdate_Click(object sender, RoutedEventArgs e)
{
// 启动一个循环更新 Tile 通知的任务,并指定 Tile 通知的数据源和轮询周期
TileUpdater tileUpdater = TileUpdateManager.CreateTileUpdaterForSecondaryTile(TILEID);
tileUpdater.EnableNotificationQueue(true); // 启用 tile 的队列功能(最多可容纳 5 个 tile) // 马上请求服务端获取数据,然后 45 分钟之后再次获取数据,最后再每半个小时获取一次数据
tileUpdater.StartPeriodicUpdate(new Uri("http://localhost:44914/api/TileContent", UriKind.Absolute), DateTimeOffset.UtcNow.AddMinutes(), PeriodicUpdateRecurrence.HalfHour); // Tile 通知的数据源示例请参见 WebApi/Controllers/TileContentController.cs
}
}
}
/WebApi/Controllers/TileContentController.cs(服务端代码)
/*
* 用于演示“轮询服务端以更新 tile 通知”的服务端部分
*/ using System;
using System.Net.Http;
using System.Text;
using System.Web.Http; namespace WebApi.Controllers
{
public class TileContentController : ApiController
{
private Random _random = new Random(); [HttpGet]
public HttpResponseMessage Get()
{
string tileXml = $@"
<tile>
<visual>
<binding template='TileSmall'>
<text>Small(小){DateTime.Now.ToString("HH:mm:ss")}</text>
</binding>
<binding template='TileMedium'>
<text>Medium(中){DateTime.Now.ToString("HH:mm:ss")}</text>
</binding>
<binding template='TileWide'>
<text>Wide(宽){DateTime.Now.ToString("HH:mm:ss")}</text>
</binding>
<binding template='TileLarge'>
<text>Large(大){DateTime.Now.ToString("HH:mm:ss")}</text>
</binding>
</visual>
</tile>"; HttpResponseMessage result = new HttpResponseMessage
{
Content = new StringContent(tileXml, Encoding.UTF8, "text/html")
}; // Tile 通知的过期时间,超过这个时间就会清除这个 Tile(对于“轮询服务端以更新 Tile 通知”来说,如果不指定此值,则默认 3 天后过期)
// 通过 ToString("R") 可以把日期格式化为“RFC 1123”格式
result.Headers.Add("X-WNS-Expires", DateTime.UtcNow.AddSeconds().ToString("R")); // 60 秒后过期 // 在启用 tile 的队列功能时,如果 tile 的 Tag 相同则新的内容会更新旧的内容(Tag 值的前 16 个字符相同则认为是相同的 Tag)
// 不指定的 Tag 的话则认为 Tag 都是不同的
result.Headers.Add("X-WNS-Tag", _random.Next().ToString()); return result;
}
}
}
OK
[源码下载]
背水一战 Windows 10 (109) - 通知(Tile): 按计划显示 tile 通知, 轮询服务端以更新 tile 通知的更多相关文章
- 背水一战 Windows 10 (112) - 通知(Badge): application 的 badge 通知, secondary 的 badge 通知, 轮询服务端以更新 badge 通知
[源码下载] 背水一战 Windows 10 (112) - 通知(Badge): application 的 badge 通知, secondary 的 badge 通知, 轮询服务端以更新 bad ...
- 背水一战 Windows 10 (103) - 通知(Toast): 基础, 按计划显示 toast 通知
[源码下载] 背水一战 Windows 10 (103) - 通知(Toast): 基础, 按计划显示 toast 通知 作者:webabcd 介绍背水一战 Windows 10 之 通知(Toast ...
- 背水一战 Windows 10 (113) - 锁屏: 将 Application 的 Badge 通知和 Tile 通知发送到锁屏, 将 secondary tile 的 Badge 通知和 Tile 通知发送到锁屏
[源码下载] 背水一战 Windows 10 (113) - 锁屏: 将 Application 的 Badge 通知和 Tile 通知发送到锁屏, 将 secondary tile 的 Badge ...
- 背水一战 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 (108) - 通知(Tile): application tile 基础, secondary tile 基础
[源码下载] 背水一战 Windows 10 (108) - 通知(Tile): application tile 基础, secondary tile 基础 作者:webabcd 介绍背水一战 Wi ...
- 背水一战 Windows 10 (118) - 后台任务: 后台下载任务(任务分组,并行或串行执行,组完成后通知)
[源码下载] 背水一战 Windows 10 (118) - 后台任务: 后台下载任务(任务分组,并行或串行执行,组完成后通知) 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 ...
- 背水一战 Windows 10 (121) - 后台任务: 推送通知
[源码下载] 背水一战 Windows 10 (121) - 后台任务: 推送通知 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 推送通知 示例演示如何接收推送通知/WebA ...
- 背水一战 Windows 10 (107) - 通知(Toast): 提示音, 特定场景
[源码下载] 背水一战 Windows 10 (107) - 通知(Toast): 提示音, 特定场景 作者:webabcd 介绍背水一战 Windows 10 之 通知(Toast) 提示音 特定场 ...
随机推荐
- CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-总目录
CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-总目录: 0.Windows 10本机下载Xshell,以方便往Linux主机上上传大文件 1.CentOS7+CDH5.14.0安 ...
- mysql数据库优化(转)
今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序员需要去关注的事情.当我们去设计数据库表结构,对操作数据 ...
- web配置详细解释
<?xml version="1.0"?> <!--注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来配置应用程序的设置.可以使用 Visual S ...
- Python播放、关闭音乐代码
1.安装pygame:win + r :打开控制台输入:pip install pygame 2.#导入 import time import pygame 3.设置音乐绝对路径 #音乐路径 file ...
- 数据结构树之AVL树(平衡二叉树)
一 什么是AVL树(平衡二叉树): AVL树本质上是一颗二叉查找树,但是它又具有以下特点:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树.在AVL树中任何节 ...
- php使用redis的GEO地理信息类型
redis3.2中增中了对GEO类型的支持,该类型存储经纬度,提供了经纬设置,查询,范围查询,距离查询,经纬度hash等操作. <?php $redis = new Redis(); $redi ...
- 在Linux系统上安装Compose
在Linux上,您可以从GitHub上的Compose存储库发行页面下载Docker Compose二进制文件.按照链接中的说明进行操作,该链接涉及curl在终端中运行命令以下载二进制文件.这些分步说 ...
- 教程:使用cPanel管理域名和数据库
cPanel是一个基于web的基于web的控制面板,它简化了许多常见的系统管理任务,如网站创建.数据库部署和管理等.本指南向您展示了如何使用cPanel用户帐户管理域和数据库.所有这些指令都与位于端口 ...
- P2158 [SDOI2008] 仪仗队(欧拉函数模板)
题目描述 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线所及的学生人数来判断队伍是否整齐(如下图 ...
- android studio 模拟器不能使用的解决方案
1.安装模拟器的时候 AS提示是 VT -x is disable 进入电脑的 bios 系统设置,怎么进入--> 在开机的时候点击F2(华硕电脑,不同电脑方式不同) --在“configura ...