Windows8开发之动态磁贴和辅助磁贴

在应用程序清单中,必须包含一张正方形的的 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内容,可以根据微软的模版,进行修改。

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

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

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

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

   

 
 
 
  1. private void UpdateTileText(string key,string[] array)  //array数组有4个元素,分别显示四行数据 
  2.     { 
  3.         try 
  4.         { 
  5.             if (array != null && array.Length > 1) 
  6.             { 
  7.                 //创建方形磁帖模板,ITileSquareText02上面是一个较大文本的标题字符串,下面是一个最多可包含三行自动换行常规文本的字符串。      
  8.                 ITileSquareText02 squareContent = TileContentFactory.CreateTileSquareText02(); 
  9.                 squareContent.TextHeading.Text = key; 
  10.                 squareContent.TextBodyWrap.Text = array[0]; 
  11.                 //创建宽模板,ITileWideSmallImageAndText02左侧是一个较小的图像,右侧上面是第一行上较大文本的标题字符串,下面是四行四个常规文本的字符串。文本不自动换行。     
  12.                 ITileWideSmallImageAndText02 tileContent = TileContentFactory.CreateTileWideSmallImageAndText02(); 
  13.                 tileContent.Image.Src = "ms-appx:///Assets/Logo.png"; 
  14.                 tileContent.TextHeading.Text = word; 
  15.                 for (int i = 0; i < array.Length; i++) 
  16.                 { 
  17.                     switch (i) 
  18.                     { 
  19.                         case 0: 
  20.                             tileContent.TextBody1.Text = array[i];  //第一行数据 
  21.                             break; 
  22.                         case 1: 
  23.                             tileContent.TextBody2.Text = array[i];  //第二行数据 
  24.                             break; 
  25.                         case 2: 
  26.                             tileContent.TextBody3.Text = array[i];  //第三行数据 
  27.                             break; 
  28.                         case 3: 
  29.                             tileContent.TextBody4.Text = array[i];  //第四行数据 
  30.                             break; 
  31.                     } 
  32.                 } 
  33.                 tileContent.SquareContent = squareContent; 
  34.              TileUpdateManager.CreateTileUpdaterForApplication().Update(tileContent.CreateNotification());                 
  35.  TileUpdateManager.CreateTileUpdaterForApplication().EnableNotificationQueue(true);                
  36.             } 
  37.         } 
  38.         catch 
  39.         { 
  40.         } 

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

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

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

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

 
 
 
  1. public Rect GetElementRect(FrameworkElement element)   
  2. {   
  3.    GeneralTransform buttonTransform = element.TransformToVisual(null);   
  4.    Point point = buttonTransform.TransformPoint(new Point());   
  5.    return new Rect(point, new Size(element.ActualWidth, element.ActualHeight));   
  6.  }  

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

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

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

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

【编辑推荐】

  1. 微软正式发布Visual Studio 2012 应用软件开发新方式
  2. Visual Studio 2012远程调试功能的改进
  3. Visual Studio动态代码生成的实现基础
  4. 微软发布Visual Studio 2012示例代码浏览器
  5. 微软已发布Visual Studio 2012的首个更新

责任编辑:彭凡
来源: 博客园 Windows 8开发

本文标题:Windows8开发之动态磁贴和辅助磁贴
标题来源:http://www.shufengxianlan.com/qtweb/news38/39238.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联