根据用户固定的磁贴大小更新动态磁贴

Update Live Tile depending on tile size pinned by user

我的 UWP 应用程序中的动态磁贴出现问题。 Microsoft 为不同的磁贴大小提供了不同的模板 (MSDN) 来设置内容,但这也取决于设备如何显示内容。

例如,宽磁贴在移动设备上显示的字符比在台式机上显示的字符多,但我想将大部分磁贴区域用于信息显示。假设用户已在台式计算机上安装了我的应用程序,并且他已将大方形磁贴固定到他的开始菜单。如何检测图块大小以加载适当的模板?基本上我只是想根据用户固定的图块使用不同的模板(然后我想根据使用的设备处理内容的填充,但我已经涵盖了)。

目前我只使用一个宽模板,如果用户固定了除宽模板以外的任何其他尺寸,该模板将不执行任何操作。如果用户固定宽磁贴,它就可以工作。但我正在努力为这个问题找到一个通用的解决方案。我正在使用 SheduledTileNotifications,因为我的应用程序仅使用本地数据作为磁贴内容。

这是我使用给定模板更新磁贴的代码:

public static void UpdatePrimaryTile(XmlDocument tileTemplate)
{
    var dt = DateTime.Now.AddSeconds(5);
    dt = DateTime.SpecifyKind(dt, DateTimeKind.Unspecified);
    var not = new ScheduledTileNotification(tileTemplate, new DateTimeOffset(dt, TimeZoneInfo.Local.BaseUtcOffset));
    var tu = TileUpdateManager.CreateTileUpdaterForApplication();
    tu.EnableNotificationQueue(true);
    tu.Clear();
    tu.AddToSchedule(not);
}

您应该在 tileTemplate 中包含所有图块尺寸,而不仅仅是宽图块。这样,无论用户选择什么,都会有一个合适的图块来显示。

仅显示当前选择的图块模板是不够的,因为用户可以在您的通知触发后更改它。

如果您查看 https://docs.microsoft.com/en-us/windows/uwp/controls-and-patterns/tiles-and-notifications-create-adaptive-tiles 上的自适应磁贴文档,它说:

For a single tile notification XML payload, provide elements for each tile size that you'd like to support, as shown in this example:

<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>

这同样适用于 Windows 8.1 模板,如 Quickstart: Sending a tile update (XAML)

中所示

The user can resize your tile on the Start screen at any time, and there is no way for you to know which state (small, medium, wide, or large) the tile is in when you send a notification.