Windows 8 应用开发 - 磁贴

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

screenshot_11172012_175541

     对于磁贴通知推送主要用到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);

图示

image

image

image

image

源码下载

http://sdrv.ms/WhVSLU

posted @   Gnie  阅读(3817)  评论(4编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
Copyright © 2010 Gnie
点击右上角即可分享
微信分享提示