原文: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

Windows 8 应用开发 - 磁贴的更多相关文章

  1. Windows phone应用开发[17]-xap提交异常处理

    在windows phone 应用提交操作上早在2011年时就写过一篇Windows phone 应用开发[4]-应用发布,那时wp应用提交官方市场的流程繁杂[超过了5步].因为上传和填写应用信息页面 ...

  2. 菜鸟学Windows Phone 8开发(4)——设置应用程序样式

    本系列文章来源MSDN的 面向完全新手的 Windows Phone 8 开发  本文地址:http://channel9.msdn.com/Series/Windows-Phone-8-Develo ...

  3. 【本人译作推荐】Windows 8应用开发:C#和XAML卷(原名:Building Windows 8 Apps with C# and XAML)

    [图书推荐] 译名:Windows 8应用开发:C#和XAML卷 原名:Building Windows 8 Apps with C# and XAML   编辑推荐 国内第一本使用XAML与C#语言 ...

  4. Windows 网络通讯开发

    Windows 网络通讯开发 一.Windows网络开发API 由于C++标准库中没有网络库,所以进行网络开发的时候要调用系统API.Windows通讯开发API包括以下几个基本函数及成员类型: 1. ...

  5. 《Windows IoT 应用开发指南》

    物物互联的时代已经到来,智能家居.智慧校园.智慧交通.可穿戴.无人机.全息投影,各种各样的新名词.黑科技层出不穷.当我们为五年前能够通过手机控制家电而欣喜若狂的时候,可曾憧憬过当前使用增强现实设备完成 ...

  6. Windows 10 IoT Serials 1 - 针对Minnow Board MAX的Windows 10 IoT开发环境搭建

    目前,微软针对Windows IoT计划支持的硬件包括树莓派2,Minnow Board MAX 和Galileo (Gen 1和Gen 2).其中,Galileo (Gen 1和Gen 2)运行的是 ...

  7. Windows phone应用开发[22]-再谈下拉刷新

    几周之前在博客更新一篇Windows phone应用开发[18]-下拉刷新 博文,有很多人在微博和博客评论中提到了很多问题.其实在实际项目中我基于这篇博文提出解决问题思路优化了这个解决方案.为了能够详 ...

  8. Windows phone应用开发[19]-RSA数据加密

    在这个系列的第十六章节中Windows phone应用开发[16]-数据加密 中曾详细讲解过windows phone 常用的MD5,HMAC_MD5,DES,TripleDES[3DES] 数据加密 ...

  9. Windows搭建python开发环境,python入门到精通[一]

    从大学开始玩python到现在参加工作,已经有5年了,现在的公司是一家.net的公司用到python的比较少,最近公司有新项目需要用到python,领导希望我来跟其他同事training,就有了这篇博 ...

随机推荐

  1. PHP正则表达式完全手册

    原文:PHP正则表达式完全手册 php的正则表达式完全手册 前言 正则表达式是烦琐的,但是强大的,学会之后的应用会让你除了提高效率外,会给你带来绝对的成就感.只要认真去阅读这些资料,加上应用的时候进行 ...

  2. linux kernel的函数与抽象层

    在数学领域,函数是一种关系,这种关系使一个集合里的每一个元素对应到另一个(可能相同的)集合里的唯一元素. 在C语言中函数也有这种联系.自变量影响着因变量. 在linux内核驱动编程经常会有抽象层的概念 ...

  3. Codeforces 474B Worms 二分法(水

    主题链接:http://codeforces.com/contest/474/problem/B #include <iostream> #include <cmath> #i ...

  4. Delphi XE中类成员的访问权限(新增了strict private和strict protected,还有automated)

    Delphi XE中类成员的访问权限共提供了6个关键词来用于限定访问权限:public.private.protected.published.automated strict private . s ...

  5. nginx 区分pc和mobile 到不同的404页面

    if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry|vivo)') { set $mobile_request '1'; } ...

  6. CString的部分实现剖析

    一.CString初探: 在CString的实现中,其最基础的类结构如下: CString其实只有一个数据成员m_pszData,这个成员指向了字符串的首地址.但在MFC的具体实现中, m_pszDa ...

  7. [WebGL入门]十,矩阵计算和外部库

    注:文章译自http://wgld.org/,原作者杉本雅広(doxas),文章中假设有我的额外说明,我会加上[lufy:],另外,鄙人webgl研究还不够深入,一些专业词语,假设翻译有误,欢迎大家指 ...

  8. poj1243(经典dp)

    题目链接:http://poj.org/problem?id=1243 题意:让你猜一个物品的价格,猜低了或者猜高了都会提示你.G,L,表示你有G次机会猜一个数,如果猜错了,G会减少1次,如果你的错误 ...

  9. android获取文件getMimeType的两种方法

    方法1: import java.util.Locale; private static String getSuffix(File file) { if (file == null || !file ...

  10. xcode Workspaces

    A workspace is an Xcode document that groups projects and other documents so you can work on them to ...