1、Pin Tile
应用程序的二级 tile ,可以在用户确认后钉到开始菜单。
运行界面:
按钮:
单击后,弹出对话框,填写显示在 tile 上面的文字:
点击 “固定到开始屏幕” 按钮,则次级 tile 可以再开始菜单上看到了:
xaml :
//按钮,触发把二级 tile 钉到桌面的命令 <Button x:Name="PinButton" Content="Pin to Start" Margin="0,0,10,0" Click="PinButton_Click"/>
相应的 C# :
public const string logoSecondaryTileId = "SecondaryTile.Logo"; private async void PinButton_Click(object sender, RoutedEventArgs e) { //为二级 tile 准备应用程序包中的两张图片(Tile Logo 和 small Logo) Uri logo = new Uri("ms-appx:///Assets/squareTile-sdk.png"); Uri smallLogo = new Uri("ms-appx:///Assets/smallTile-sdk.png"); // 在创建 二级 tile 时,需要为这个 tile 添加额外的参数,从而当这个 tile 被激活时传递给应用程序 // 这些参数对于这个应用应该是有意义的。在本示例中,我们会传递当前时间和日期 string tileActivationArguments = logoSecondaryTileId + " WasPinnedAt=" + DateTime.Now.ToLocalTime().ToString(); // 创建一个 1x1 的二级 tile SecondaryTile secondaryTile = new SecondaryTile(logoSecondaryTileId, "Title text shown on the tile", "Name of the tile the user sees when searching for the tile", tileActivationArguments, TileOptions.ShowNameOnLogo, logo); // 指定一个前景文本值。 // 二级 tile 的背景继承自父元素,除非指定一个单独的值 secondaryTile.ForegroundText = ForegroundText.Dark; // 和背景色类似,这个小标题(small tile logo)默认也是从父 tile 那里继承的。我们重写它,只需要使用下面的步骤 secondaryTile.SmallLogo = smallLogo; // 好了,这个 tile 创建完成了,我们现在尝试把它钉到桌面 // 注意到当异步操作(钉 tile)完成时,消息状态便会更新
// 显示指定矩形给定边上的辅助图块的预览,以及创建图块的确认对话框。 bool isPinned = await secondaryTile.RequestCreateForSelectionAsync(GetElementRect((FrameworkElement)sender),
Windows.UI.Popups.Placement.Below); if (isPinned) { //Secondary tile successfully pinned } else { //Secondary tile not pinned } }
//得到元素的矩形表示 public static Rect GetElementRect(FrameworkElement element) { GeneralTransform buttonTransform = element.TransformToVisual(null); Point point = buttonTransform.TransformPoint(new Point()); return new Rect(point, new Size(element.ActualWidth, element.ActualHeight)); }
2、本示例讲述如何去掉(upin) 1、中 钉到开始菜单上面的 tile。如果是真是的项目中,我们应该首先判断这个 tile 是否存在。在 4)中讲述了如何
判断 指定的 tile 是否存在。
按钮截图:
单击按钮后:
点击 “从开始屏幕取消固定”后,该 tile 消失。
xaml:
//Unpin 这个 tile <Button x:Name="UnpinSecondaryTile" Content="Unpin from Start" Margin="0,0,10,0" Click="UnpinSecondaryTile_Click"/>
对应的 C# :
public const string logoSecondaryTileId = "SecondaryTile.Logo"; private async void UnpinSecondaryTile_Click(object sender, RoutedEventArgs e) { // 检查调用应用程序是否存在特定辅助图块。 if (Windows.UI.StartScreen.SecondaryTile.Exists(logoSecondaryTileId)) { // 准备即将被取消固定的 tile SecondaryTile secondaryTile = new SecondaryTile(logoSecondaryTileId); // 取消固定 tile bool isUnpinned = await secondaryTile.RequestDeleteForSelectionAsync(GetElementRect((FrameworkElement)sender),
Windows.UI.Popups.Placement.Below); if (isUnpinned) { //succeed } else { // fail } } else { // 没有被 pin 到开始菜单 } }
public static Rect GetElementRect(FrameworkElement element) { GeneralTransform buttonTransform = element.TransformToVisual(null); Point point = buttonTransform.TransformPoint(new Point()); return new Rect(point, new Size(element.ActualWidth, element.ActualHeight)); }
3、Enumerate Tiles :
本示例获取应用程序的所有 二级 tile 的列表。当更新通知的频道(channel) URIs时,或者当程序第一次在 pc 上运行打开 频道(channels)时
很有用。
应用界面的按钮:
单击后输出:
xaml :
//列举出所有的二级 tile <Button x:Name="EnumerateSecondaryTiles" Content="Enumerate tiles" Margin="0,0,10,0" Click="EnumerateSecondaryTiles_Click"/> //显示结果 <TextBlock x:Name="OutputTextBlock" Margin="0,0,0,5" />
private async void EnumerateSecondaryTiles_Click(object sender, RoutedEventArgs e) { Button button = sender as Button; if (button != null) { // 检索为调用应用程序创建的辅助图块列表。
IReadOnlyList<SecondaryTile> tilelist = await Windows.UI.StartScreen.SecondaryTile.FindAllAsync();
if (tilelist.Count > 0) { int count = 0; StringBuilder outputText = new StringBuilder(); foreach (var tile in tilelist) { outputText.AppendFormat("Tile Id[{0}] = {1}, Tile short display name = {2} {3}",
count++, tile.TileId, tile.ShortName, System.Environment.NewLine); } OutputTextBlock.Text = outputText.ToString(); } else { //没有二级 tile } } }
4、Is Tile Pined :
//判断指定 id 的 tile 是否存在 bool IsExists = Windows.UI.StartScreen.SecondaryTile.Exists(SecondaryTileId);
5、Show Activation Arguments :
本示例讲述当用户点击二级 tile 时,这个二级 tile 可以传递参数给应用程序。这些参数是在二级 tile 被创建时设置的。当这个 tile 被激活时,使用一个事件
接收并处理。
点击 1、 中 pin 到开始菜单中的 tile,显示接收的结果是设置此 tile ( logoSecondaryTileId = "SecondaryTile.Logo"; )时,加入的参数。
显示结果截图:
在 App 类的 OnLaunched(LaunchActivatedEventArgs args) 事件中,接收该二级 tile 传递来的参数 args,赋值给一个自定义的全局的参数:
public Windows.ApplicationModel.Activation.LaunchActivatedEventArgs LaunchArgs;
在其它页面显示这个参数 :
txtblock.Text = "Application was activated from a Secondary Tile with the following Activation Arguments : "
+ LaunchArgs.Arguments,
6、Secondary Tile Notification :
二级 tiles 也可以跟应用的 tile 一样,发送 tile 和 badge 通知。
运行截图:
点击 “Pin to Start ” 按钮,添加一个用来显示通知的二级 tile:
固定这个 二级的tile,显示在开始屏幕上面:
点击按钮(“Send Tile Notification”)发送 tile 通知,显示结果:
点击按钮(“Send Badge Notification”):
xaml 代码:
//创建二级 tile <Button x:Name="PinLiveTile" Content="Pin to Start" Click="PinLiveTile_Click"/> //发送 Tile 通知 <Button x:Name="SendTileNotification" Content="Send Tile Notification" Click="SendTileNotification_Click"/> //发送 Badge 通知 <Button x:Name="SendBadgeNotification" Content="Send Badge Notification" Click="SendBadgeNotification_Click"/>
相应的 C# :
把一个二级 tile 固定到开始屏幕:
public const string dynamicTileId = "SecondaryTile.LiveTile"; private async void PinLiveTile_Click(object sender, RoutedEventArgs e) { Uri logo = new Uri("ms-appx:///Assets/squareTile-sdk.png"); Uri wideLogo = new Uri("ms-appx:///Assets/tile-sdk.png"); string tileActivationArguments = dynamicTileId + " WasPinnedAt=" + DateTime.Now.ToLocalTime().ToString(); //TileOptions.ShowNameOnLogo : 在图块的方形版本上显示名称。 // TileOptions.ShowNameOnWideLogo : 在图块的宽型版本上显示名称。 SecondaryTile secondaryTile = new SecondaryTile(dynamicTileId, "A Live Secondary Tile", "Secondary Tile Sample Live Secondary Tile", tileActivationArguments, TileOptions.ShowNameOnLogo | TileOptions.ShowNameOnWideLogo, logo, wideLogo); secondaryTile.ForegroundText = ForegroundText.Light; bool isPinned = await secondaryTile.RequestCreateForSelectionAsync(
GetElementRect((FrameworkElement)sender), Windows.UI.Popups.Placement.Below); if (isPinned) { //Pin Succeed } else { //Pin fail } } public static Rect GetElementRect(FrameworkElement element) { GeneralTransform buttonTransform = element.TransformToVisual(null); Point point = buttonTransform.TransformPoint(new Point()); return new Rect(point, new Size(element.ActualWidth, element.ActualHeight)); }
发送 Tile 通知 (需要引用扩展库 : NotificationsExtensions):
private void SendTileNotification_Click(object sender, RoutedEventArgs e) { if (SecondaryTile.Exists(dynamicTileId)) { ITileWideText04 tileContent = TileContentFactory.CreateTileWideText04(); //显示的通知文字 tileContent.TextBodyWrap.Text = "Sent to a secondary tile from NotificationsExtensions!"; ITileSquareText04 squareContent = TileContentFactory.CreateTileSquareText04(); squareContent.TextBodyWrap.Text = "Sent to a secondary tile from NotificationExtensions!"; tileContent.SquareContent = squareContent; //通过创建一个二级 tile 更新器 (secondary tile updater) 来发送通知 //创建并初始化 TileUpdater 的新实例,此操作可让您更改 secondary tile 的外观。 //平铺可属于调用应用程序或相同包中的其他任何应用程序。 TileUpdateManager.CreateTileUpdaterForSecondaryTile(dynamicTileId).Update(tileContent.CreateNotification()); //成功发送通知到 id 为 dynamicTileId 的 tile } else { // fail } }
发送 Badge 通知(需要引用扩展库 : NotificationsExtensions):
private void SendBadgeNotification_Click(object sender, RoutedEventArgs e) { if (SecondaryTile.Exists(dynamicTileId)) { //参数为显示在 tile 上面的数字。如果该参数为 0,该 badge 会被移除。显示在 tile 上的最大数字是 “99” //,如果数字比 99 大的话,会显示 “99+” BadgeNumericNotificationContent badgeContent = new BadgeNumericNotificationContent(6);
//发送通知到这个 secondary tile BadgeUpdateManager.CreateBadgeUpdaterForSecondaryTile(dynamicTileId).Update(badgeContent.CreateNotification()); // "Badge notification " 发送成功 } else { // 开始菜单没有 id 为 dynamicTileId 的 Tile } }
发送 Tile 和 Badge 通知,使用自定义的构造 tile 的字符串 :
private void SendTileNotificationWithStringManipulation_Click(object sender, RoutedEventArgs e) { string tileXmlString = "<tile>" + "<visual>" + "<binding template='TileWideText04'>" + "<text id='1'>Send to a secondary tile from strings</text>" + "</binding>" + "<binding template='TileSquareText04'>" + "<text id='1'>Send to a secondary tile from strings</text>" + "</binding>" + "</visual>" + "</tile>"; Windows.Data.Xml.Dom.XmlDocument tileDOM = new Windows.Data.Xml.Dom.XmlDocument(); tileDOM.LoadXml(tileXmlString); TileNotification tile = new TileNotification(tileDOM); // Send the notification to the secondary tile by creating a secondary tile updater TileUpdateManager.CreateTileUpdaterForSecondaryTile(dynamicTileId).Update(tile); } private void SendBadgeNotificationWithStringManipulation_Click(object sender, RoutedEventArgs e) { string badgeXmlString = "<badge value='9'/>"; Windows.Data.Xml.Dom.XmlDocument badgeDOM = new Windows.Data.Xml.Dom.XmlDocument(); badgeDOM.LoadXml(badgeXmlString); BadgeNotification badge = new BadgeNotification(badgeDOM); // Send the notification to the secondary tile BadgeUpdateManager.CreateBadgeUpdaterForSecondaryTile(dynamicTileId).Update(badge); }
7、Update Secondary Tile Default Logo:
本示例讲述如何更改一个二级 tile 的 logo,比较简单,仅上代码:
public const string logoSecondaryTileId = "SecondaryTile.Logo"; private async void UpdateDefaultLogo_Click(object sender, RoutedEventArgs e) { if (Windows.UI.StartScreen.SecondaryTile.Exists(logoSecondaryTileId)) { // 使用特定 ID 创建 SecondaryTile 对象。 SecondaryTile secondaryTile = new SecondaryTile(logoSecondaryTileId); //添加需要更新 tile 的属性(本示例是 logo) secondaryTile.Logo = new Uri("ms-appx:///Assets/squareTileLogoUpdate-sdk.png"); // 图块固定到“启动”屏幕后,更新辅助图块的默认徽标。 bool isUpdated = await secondaryTile.UpdateAsync(); if (isUpdated) { //logo 更新成功 } else { // logo 更新失败 } } else { //该二级 tile 不存在 } }