UWP 磁贴设置

一:需求

一款好看好用的应用,对于UWP来说,动态的磁贴必不可少。

二:TileUpdateManager类 和TileUpdater类

如果需要更改或更新应用的磁贴,那么首先需要获得TileUpdater对象,顾名思义,TileUpdater就是磁贴的更新器。

通过TileUpdateManager类得到TileUpdater对象

TileUpdater tileUpdater = TileUpdateManager.CreateTileUpdaterForApplication();

三:TileUpdater 磁贴更新器类

    3.1 清空应用的磁贴 

    TileUpdater 有一个clear()方法用于清空当前应用的磁贴的内容,恢复为默认模式,默认就是一个软件的logo。该方法可以用于软件内点击按钮关闭动态磁贴的功能。

//删除所有更新并将平铺导致以显示其默认内容(如应用程序清单所声明)。
public void Clear();
tileUpdater.Clear();

 3.2  更新磁贴

 TileUpdater 有一个Update() 方法,用于更新磁贴,可以说是比较关键的方法了。先来看看方法的定义。

// 将内容或外观的更改应用于图块。
// notification:
// 为平铺的内容提供新的 XML 定义的对象。
public void Update(TileNotification notification);

参数需要提供一个 TileNotification 对象。查看TileNotification类的构造函数可以发现

public TileNotification(XmlDocument content);
TileNotification类的构造函数需要传递进一个XmlDocument对象。

  实际上磁贴的模板就是一个xml的文件。TileNotification 对象就是用来放要更新的磁贴的模板。 

四:磁铁模板

磁贴的模板可以用微软预定的磁贴模板,或者自己定义模板。

 4.1获取微软预设的磁贴模板

XmlDocument xml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileSquare150x150Block);
TileTemplateType里有很多微软预设的磁贴的模板,主要是根据不同的磁贴大小,微软自己预设的磁贴。

4.2 创建自适应磁贴模板
自适应磁贴是win10一项新的功能,允许我们针对不同的磁贴大小设定模板。

既然磁贴的模板是xml格式的,那么它就是标记性语言。你可以像xaml那样,或者用类的方式定义。
请安装名为 Microsoft.Toolkit.Uwp.Notifications 的 NuGet 程序包(搜索“notifications uwp”)


xml格式:
<tile>
  <visual>

    <binding template="TileSmall">
      <text>Small</text>
    </binding>

    <binding template="TileMedium">
      <text>Medium</text>
    </binding>

    <binding template="TileWide">
      <text>Wide</text>
    </binding>

    <binding template="TileLarge">
      <text>Large</text>
    </binding>

  </visual>
</tile>

 

 c#格式

TileContent content = new TileContent()
{
    Visual = new TileVisual()
    {
        TileSmall = new TileBinding()
        {
            Content = new TileBindingContentAdaptive()
            {
                Children =
                {
                    new AdaptiveText() { Text = "Small" }
                }
            }
        },

        TileMedium = new TileBinding()
        {
            Content = new TileBindingContentAdaptive()
            {
                Children =
                {
                    new AdaptiveText() { Text = "Medium" }
                }
            }
        },

        TileWide = new TileBinding()
        {
            Content = new TileBindingContentAdaptive()
            {
                Children =
                {
                    new AdaptiveText() { Text = "Wide" }
                }
            }
        },

        TileLarge = new TileBinding()
        {
            Content = new TileBindingContentAdaptive()
            {
                Children =
                {
                    new AdaptiveText() { Text = "Large" }
                }
            }
        }
    }
};

 

这个自适应的磁贴也仅仅是针对四种状体的磁贴定义的。因为微软的磁贴也只能有(小,中,宽,大)四种形态。分别对应(TileSmall,TileMedium ,TileWide,TileLarge)。在每种状态里,添加你需要内容即可。你可以添加PeekImage,BackgroundImage,AdaptiveText等。

 

 

 

如果你用的是c#写的磁贴模板,有一个TileContent.Getxml() 方法用于将c#代码转化成xml。

这样你再创建一个TileNotification对象,通过TileUpdater.update()就可以更新磁贴了。

var xmlDocument= content.GetXml();

TileNotification tileNotification = new TileNotification(xmlDocument);
TileUpdater tileUpdater
= TileUpdateManager.CreateTileUpdaterForApplication(); tileUpdater.Update(tileNotification);

 

微软开发者文档地址:https://docs.microsoft.com/zh-cn/windows/uwp/controls-and-patterns/tiles-and-notifications-create-adaptive-tiles

 

 

 

-------some words-------

1.Notification 通知

2..tile 磁贴

---------the  end----------

posted @ 2017-10-31 21:27  5只猫  阅读(1551)  评论(0编辑  收藏  举报