Windows 8开发之动态磁贴和辅助磁贴

开发 后端
磁贴在系统的开始菜单中代表着应用,动态磁贴可以向用户显示新的、重大的、定制的内容。 Tile 通知是一种固定格式的 XML,其中包含文字和图片内容。

在应用程序清单中,必须包含一张正方形的的 logo,如果 应用程序也想使用宽模版 logo,也需要在清单中注明。如果你的应用中同样支持宽 tile,强烈建议你预加载 方形和宽形 在预加 载的 xml 中,从而无论开始菜单中的 tile 是方形或者 宽形的 都可以接收通知。

以下是微软提供的磁贴模版:

磁贴和磁贴通知 : http://msdn.microsoft.com/zh-cn/library/windows/apps/hh779724.aspx

磁贴模板目录 :    http://msdn.microsoft.com/zh-cn/library/windows/apps/hh761491.aspx

1.动态磁贴

  使用 Windows.UI.Notifications 命名空间下的 TileUpdateManager 类, 创建用于更改和更新启动菜单图块的 TileUpdater对象。此类提供对系统提供的平铺模板 XML 内容的访问,以便您可以自定义用于更新您平铺的内容。 具体模版的XML内容,可以根据微软的模版,进行修改。

public static class TileUpdateManager 
   { 
       // 摘要: 
       //     创建并初始化 TileUpdater 的新实例,此操作可让您更改调用应用程序图块的外观。 
       // 
       // 返回结果: 
       //     用于将更改发送到应用程序的平铺的对象。 
       [Overload("CreateTileUpdaterForApplication")] 
       public static TileUpdater CreateTileUpdaterForApplication(); 
       // 
       // 摘要: 
       //     创建并初始化图块 TileUpdater 的新实例,该图块属于和调用应用程序位于同一包中的另一应用程序。TileUpdater 允许开发人员更改该平铺外观。 
       // 
       // 参数: 
       //   applicationId: 
       //     标题的唯一 ID。 
       // 
       // 返回结果: 
       //     用于通过 applicationId 将更改发送到平铺标识的对象。 
       [Overload("CreateTileUpdaterForApplicationWithId")] 
       public static TileUpdater CreateTileUpdaterForApplication(string applicationId); 
       // 
       // 摘要: 
       //     创建并初始化 TileUpdater 的新实例,此操作可让您更改 secondary tile 的外观。平铺可属于调用应用程序或相同包中的其他任何应用程序。 
       // 
       // 参数: 
       //   tileId: 
       //     标题的唯一 ID。 
       // 
       // 返回结果: 
       //     用于通过 tileID 将更新发送到平铺标识的对象。 
       public static TileUpdater CreateTileUpdaterForSecondaryTile(string tileId); 
      // 
       // 摘要: 
       //     获取预定义的图块模板之一的 XML 内容,这样您可以自定义该内容以进行图块更新。 
       // 
       // 参数: 
       //   type: 
       //     模板的名称。 
       // 
       // 返回结果: 
       //     包含 XML 的对象。 
       public static XmlDocument GetTemplateContent(TileTemplateType type); 
   } 
       //使用模版自定义字的符串 
       void UpdateTileButton_Click(object sender, RoutedEventArgs e) 
       { 
           string tileXmlString = "<tile>" 
                             + "<visual>" 
                             + "<binding template='TileWideImageAndText01'>" 
                             + "<text id='1'>This tile notification uses ms-appx images</text>" 
                             + "<image id='1' src='ms-appx:///images/redWide.png' alt='Red image'/>" 
                             + "</binding>" 
                             + "<binding template='TileSquareImage'>" 
                             + "<image id='1' src='ms-appx:///images/graySquare.png' alt='Gray image'/>" 
                             + "</binding>" 
                             + "</visual>" 
                             + "</tile>"
           Windows.Data.Xml.Dom.XmlDocument tileDOM = new Windows.Data.Xml.Dom.XmlDocument(); 
           tileDOM.LoadXml(tileXmlString); 
           TileNotification tile = new TileNotification(tileDOM); 
          TileUpdateManager.CreateTileUpdaterForApplication().Update(tile); 
       } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.

  另外我们可以下载NotificationsExtensions文件,将该文件引用到我们的项目中,使用该文件中提供的类和方法来创建我们的动态磁贴,这个和前面不同的地方是,我们可以不使用XML模版进行设置磁贴的模版,而是通过具体的模版类,给模版类的属性赋值,实现动态磁贴。下面的代码实现了,最近五个动态磁贴之间的切换,该模版显示的样式为:

宽磁贴:左侧是一个较小的图像,右侧上面是第一行上较大文本的标题字符串,下面是四行四个常规文本的字符串。文本不自动换行。

窄磁贴:上面是一个较大文本的标题字符串,下面是一个最多可包含三行自动换行常规文本的字符串。

   

private void UpdateTileText(string key,string[] array)  //array数组有4个元素,分别显示四行数据 
    { 
        try 
        { 
            if (array != null && array.Length > 1) 
            { 
                //创建方形磁帖模板,ITileSquareText02上面是一个较大文本的标题字符串,下面是一个最多可包含三行自动换行常规文本的字符串。      
                ITileSquareText02 squareContent = TileContentFactory.CreateTileSquareText02(); 
                squareContent.TextHeading.Text = key; 
                squareContent.TextBodyWrap.Text = array[0]; 
                //创建宽模板,ITileWideSmallImageAndText02左侧是一个较小的图像,右侧上面是第一行上较大文本的标题字符串,下面是四行四个常规文本的字符串。文本不自动换行。     
                ITileWideSmallImageAndText02 tileContent = TileContentFactory.CreateTileWideSmallImageAndText02(); 
                tileContent.Image.Src = "ms-appx:///Assets/Logo.png"
                tileContent.TextHeading.Text = word; 
                for (int i = 0; i < array.Length; i++) 
                { 
                    switch (i) 
                    { 
                        case 0: 
                            tileContent.TextBody1.Text = array[i];  //第一行数据 
                            break
                        case 1: 
                            tileContent.TextBody2.Text = array[i];  //第二行数据 
                            break
                        case 2: 
                            tileContent.TextBody3.Text = array[i];  //第三行数据 
                            break
                        case 3: 
                            tileContent.TextBody4.Text = array[i];  //第四行数据 
                            break
                    } 
                } 
                tileContent.SquareContent = squareContent; 
             TileUpdateManager.CreateTileUpdaterForApplication().Update(tileContent.CreateNotification());                 
 TileUpdateManager.CreateTileUpdaterForApplication().EnableNotificationQueue(true);                
            } 
        } 
        catch 
        { 
        } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.

 

注意:动态磁贴必须要在实体机器上,在模拟器中是无法显示动态磁贴的效果。

  2.辅助磁贴(二级磁贴)

  辅助磁贴使用户能够将 Windows 应用商店应用的特定内容和深层链接—对固定应用内部一个特定位置的引用—发送到“开始”屏幕上。辅助磁贴使用户能够使用好友、新闻源、股票报价和其他对其很重要的内容来个性化“开始”屏幕体验。创建辅助磁贴的选项最常在 UI 中显示为“附到开始菜单”选项。固定内容也就是为它创建辅助磁贴。此选项常常显示为应用栏上的一个标志符号。通过触摸或单击来选择辅助磁贴,会启动到父应用,以突出一种以固定内容或联系人为中心的体验。只有用户才可以固定辅助磁贴;应用不能在未获得用户许可的情况下以编程方式固定辅助磁贴。用户还可以通过“开始”屏幕或通过父应用,对辅助磁贴进行显式删除控制。

  1).在固定辅助磁贴前,用户必须确认,要求对此进行确认的弹出窗口应当显示在调用固定请求的按钮旁边。

 

public 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));   
 }  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

 

  2).添加辅助磁贴。首先需要设置辅助磁贴的ID

 public const string appbarTileId = "SecondaryTile.AppBar"
 private async void AddButtonClicked(object sender, RoutedEventArgs e)   
{   
   //当用户选择应用栏上的按钮时,会显示一个要求用户进行确认的弹出窗口。   
   //若要确保在显示弹出窗口时不取消应用栏,必须设置应用栏的 IsSticky 属性。   
   this.BottomAppBar.IsSticky = true;   
   if(SecondaryTile.Exists(appbarTileId))   
   {   
     //取消相应的辅助磁贴   
      SecondaryTile secondaryTile = new SecondaryTile(appbarTileId);   
      bool isUnpinned = await secondaryTile.RequestDeleteForSelectionAsync(GetElementRect((FrameworkElement)sender), Windows.UI.Popups.Placement.Above);   
      ToggleAppBarButton(isUnpinned);   
   }   
    else  
   {   
        //辅助磁贴的一些属性需要设置后才能固定辅助磁贴.   
        //•磁贴的唯一 ID   
        //•短名称   
        //•显示名称   
        //•磁贴选项   
        //•徽标图像   
        //•激活辅助磁贴时将传递到父应用程序的参数字符串   
        Uri logo = new Uri("ms-appx:///Assets/logo.jpg");   
       string tileActivationArguments = appbarTileId + " was pinned at " + DateTime.Now.ToLocalTime().ToString();   
       SecondaryTile secondaryTile = new SecondaryTile(appbarTileId,   
                                                        "Secondary tile pinned via AppBar",   
                                                             "SDK Sample Secondary Tile pinned from AppBar",                                                         tileActivationArguments,                                                             TileOptions.ShowNameOnLogo,   
                               logo);   
            //指定前景文本颜色和小徽标。   
              secondaryTile.ForegroundText = ForegroundText.Dark;   
              secondaryTile.SmallLogo = new Uri("ms-appx:///Assets/small.jpg");   
             //调用异步方法将辅助磁贴固定。   
             //实际上这种方法不是将磁贴直接固定到“开始”屏幕,而是会显示一个要求用户允许这样做的确认提示框。   
              bool isPinned = await secondaryTile.RequestCreateForSelectionAsync(GetElementRect((FrameworkElement)sender), Windows.UI.Popups.Placement.Above);   
              ToggleAppBarButton(!isPinned);   
          }   
         this.BottomAppBar.IsSticky = false;   
     } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.

 

  以上就完成了,辅助磁贴的添加和显示。

原文链接:http://www.cnblogs.com/akwwl/archive/2013/02/18/2880120.html

 

【编辑推荐】

责任编辑:彭凡 来源: 博客园
相关推荐

2021-06-16 09:49:14

Windows 11微软动态磁贴

2020-03-27 18:00:37

微软Windows 10操作系统

2015-12-21 10:49:57

Windows 10开始菜单磁贴

2022-01-19 09:37:29

微软Windows11Windows

2013-04-25 14:15:53

Windows PhoWindows PhoWindows Pho

2014-12-31 16:37:16

win8磁盘自定义ImageVie

2013-04-17 11:21:59

Windows PhoWindows Pho

2022-01-18 06:28:01

Windows 11操作系统微软

2020-11-02 13:04:46

微软Windows 10网络

2019-07-25 08:19:40

Windows 10系统更新开始菜单

2021-06-16 13:08:14

微软Windows 10Windows

2021-06-28 19:58:08

微软Windows 11Windows

2022-01-17 07:10:32

Windows 11操作系统微软

2015-09-21 09:28:09

开始菜单Build 10547Windows 10

2015-10-14 10:54:25

UWP应用SDKWindows 10

2022-11-22 08:17:06

微软Windows 11

2020-03-20 20:00:05

微软Windows 10新版变化

2013-04-16 11:31:27

Windows 8.1

2013-05-30 14:00:41

Windows 8.1

2021-06-17 05:28:03

Windows11操作系统微软
点赞
收藏

51CTO技术栈公众号