原文:Windows 8 应用开发 - 磁贴

     我们开发的应用在Win8 界面中会以磁贴形式展现,默认情况下磁贴会显示应用图标,即项目工程中的Logo.png图片文件。开发人员可按应用的需要使用通知的方式将文字或图片信息推送到磁贴,从而对磁贴中显示的内容进行更换。

     对于磁贴通知推送主要用到API 是Windows.UI.Notifications,API 中提供了很多磁贴显示模版TileTemplateType,模版的结构是通过XML 描述的。其实我们需要做的就是编辑模版中的内容,然后将它推送到磁贴。有些童鞋可能发现下面代码中定义了两个模版:TileWideImageAndText01 和TileSquareText04。这是由于Win8 应用有大小两种尺寸的磁贴,在应用推送通知时无法知道当前磁贴的大小,如果推送的模版内容与磁贴尺寸不符,在显示方面可能会产生问题,所以官方建议将两种尺寸得模版全部定义好,这样无论磁贴处于哪种尺寸都不会发生显示问题。

<tile>
<visual>
<binding template="TileWideImageAndText01">
<image src="ms-appx:///Assets/Wide.png"/>
<text>This is a wide tile notification!</text>
</binding> <binding template="TileSquareText04">
<text>This is a square tile notification!</text>
</binding>
</visual>
</tile>

     接下来我们需要做的就是编辑模版内容(如下代码),先用TileUpdateManager 类的GetTemplateContent 方法定义大尺寸磁贴模版,示例中TileWideImageAndText01 是带有图片和文字的大尺寸模版,TileTemplateType 枚举还包含其他类型的模版,开发者可自行选取使用。后续使用标准BOM 编辑模版的<text> 和<image> 标签设置相应的数值和属性。

     同样,编辑完小尺寸磁贴后,将其加载到大尺寸模版<visual>(如上XML)。通过TileNotification 创建一个磁贴通知,可使用ExpirationTime 设置磁贴通知消失时间。最后,使用TileUpdateManager.CreateTileUpdaterForApplication().Update() 推送通知,可使用Clear() 清除磁贴通知。

// For wide tile
XmlDocument wideTile = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWideImageAndText01);
XmlNodeList wdTxtAttribute = wideTile.GetElementsByTagName("text");
XmlNodeList wdImgAttribute = wideTile.GetElementsByTagName("image");
((XmlElement)wdImgAttribute[0]).SetAttribute("src", "ms-appx:///Assets/Wide.png");
wdTxtAttribute[0].InnerText = "This is a wide tile notification!"; // For square tile
XmlDocument sqTile = TileUpdateManager.GetTemplateContent(TileTemplateType.TileSquareText04);
XmlNodeList sqTxtAttribute = sqTile.GetElementsByTagName("text");
sqTxtAttribute[0].InnerText = "This is a square tile notification!"; IXmlNode node = wideTile.ImportNode(sqTile.GetElementsByTagName("binding").Item(0), true);
wideTile.GetElementsByTagName("visual").Item(0).AppendChild(node); TileNotification tileNotification = new TileNotification(wideTile);
tileNotification.ExpirationTime = DateTimeOffset.UtcNow.AddSeconds(10);
TileUpdateManager.CreateTileUpdaterForApplication().Update(tileNotification);

图示

源码下载

http://sdrv.ms/WhVSLU

最新文章

  1. 烂泥:利用awstats分析nginx日志
  2. ubuntu 安装 vmware 12
  3. 按Enter键执行表单验证
  4. html5 -js判断undefined类型
  5. HDU 5536 Chip Factory 字典树
  6. 我的第一个phonegap开发WebApp的demo 怎么搭建安卓开发环境以及安装phonegap
  7. SQL Server 2008 错误 233 的解决办法
  8. 【Android】读取sdcard卡上的全部图片而且显示,读取的过程有进度条显示
  9. 前端MVC学习笔记(二)——AngularJS验证、过滤器、指令
  10. 升级:DNAtools for Excel工具箱,2.x英文版- VBA代码破解工具
  11. php函数 array_combine
  12. LinkedList浅析
  13. phpstudy 升级(更换) mysql 版本
  14. npm webpack vue-cli
  15. python advanced programming ( II )
  16. FFmpeg编程学习笔记二:音频重採样
  17. 在springboot项目中使用mybatis 集成 Sharding-JDBC
  18. 解题:POI 2007 Driving Exam
  19. css &amp; text-overflow &amp; ellipsis
  20. js中的apply、call、bind

热门文章

  1. Qt之VLFeat SLIC超像素分割(Cpp版)
  2. MySQL查看连接数
  3. 《Python学习手册》读书笔记
  4. Wi-Fi万能钥匙:说是破解,其实有危险(转)
  5. GitHub上最火的74个Android开源项目
  6. 基于libevent, libuv和android Looper不断演进socket编程 - 走向架构师之路 - 博客频道 - CSDN.NET
  7. 标准输入的原理:cin与scanf
  8. WPF案例(二)模拟Apple OS 界面前后180度反转
  9. QT怎样在QTableWidge显示图片
  10. Android之场景桌面(一)