Windows 8開發(fā)之動(dòng)態(tài)磁貼和輔助磁貼
在應(yīng)用程序清單中,必須包含一張正方形的的 logo,如果 應(yīng)用程序也想使用寬模版 logo,也需要在清單中注明。如果你的應(yīng)用中同樣支持寬 tile,強(qiáng)烈建議你預(yù)加載 方形和寬形 在預(yù)加 載的 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.動(dòng)態(tài)磁貼
使用 Windows.UI.Notifications 命名空間下的 TileUpdateManager 類, 創(chuàng)建用于更改和更新啟動(dòng)菜單圖塊的 TileUpdater對(duì)象。此類提供對(duì)系統(tǒng)提供的平鋪模板 XML 內(nèi)容的訪問,以便您可以自定義用于更新您平鋪的內(nèi)容。 具體模版的XML內(nèi)容,可以根據(jù)微軟的模版,進(jìn)行修改。
- public static class TileUpdateManager
- {
- // 摘要:
- // 創(chuàng)建并初始化 TileUpdater 的新實(shí)例,此操作可讓您更改調(diào)用應(yīng)用程序圖塊的外觀。
- //
- // 返回結(jié)果:
- // 用于將更改發(fā)送到應(yīng)用程序的平鋪的對(duì)象。
- [Overload("CreateTileUpdaterForApplication")]
- public static TileUpdater CreateTileUpdaterForApplication();
- //
- // 摘要:
- // 創(chuàng)建并初始化圖塊 TileUpdater 的新實(shí)例,該圖塊屬于和調(diào)用應(yīng)用程序位于同一包中的另一應(yīng)用程序。TileUpdater 允許開發(fā)人員更改該平鋪外觀。
- //
- // 參數(shù):
- // applicationId:
- // 標(biāo)題的唯一 ID。
- //
- // 返回結(jié)果:
- // 用于通過 applicationId 將更改發(fā)送到平鋪標(biāo)識(shí)的對(duì)象。
- [Overload("CreateTileUpdaterForApplicationWithId")]
- public static TileUpdater CreateTileUpdaterForApplication(string applicationId);
- //
- // 摘要:
- // 創(chuàng)建并初始化 TileUpdater 的新實(shí)例,此操作可讓您更改 secondary tile 的外觀。平鋪可屬于調(diào)用應(yīng)用程序或相同包中的其他任何應(yīng)用程序。
- //
- // 參數(shù):
- // tileId:
- // 標(biāo)題的唯一 ID。
- //
- // 返回結(jié)果:
- // 用于通過 tileID 將更新發(fā)送到平鋪標(biāo)識(shí)的對(duì)象。
- public static TileUpdater CreateTileUpdaterForSecondaryTile(string tileId);
- //
- // 摘要:
- // 獲取預(yù)定義的圖塊模板之一的 XML 內(nèi)容,這樣您可以自定義該內(nèi)容以進(jìn)行圖塊更新。
- //
- // 參數(shù):
- // type:
- // 模板的名稱。
- //
- // 返回結(jié)果:
- // 包含 XML 的對(duì)象。
- 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);
- }
另外我們可以下載NotificationsExtensions文件,將該文件引用到我們的項(xiàng)目中,使用該文件中提供的類和方法來創(chuàng)建我們的動(dòng)態(tài)磁貼,這個(gè)和前面不同的地方是,我們可以不使用XML模版進(jìn)行設(shè)置磁貼的模版,而是通過具體的模版類,給模版類的屬性賦值,實(shí)現(xiàn)動(dòng)態(tài)磁貼。下面的代碼實(shí)現(xiàn)了,最近五個(gè)動(dòng)態(tài)磁貼之間的切換,該模版顯示的樣式為:
寬磁貼:左側(cè)是一個(gè)較小的圖像,右側(cè)上面是第一行上較大文本的標(biāo)題字符串,下面是四行四個(gè)常規(guī)文本的字符串。文本不自動(dòng)換行。
窄磁貼:上面是一個(gè)較大文本的標(biāo)題字符串,下面是一個(gè)最多可包含三行自動(dòng)換行常規(guī)文本的字符串。
- private void UpdateTileText(string key,string[] array) //array數(shù)組有4個(gè)元素,分別顯示四行數(shù)據(jù)
- {
- try
- {
- if (array != null && array.Length > 1)
- {
- //創(chuàng)建方形磁帖模板,ITileSquareText02上面是一個(gè)較大文本的標(biāo)題字符串,下面是一個(gè)最多可包含三行自動(dòng)換行常規(guī)文本的字符串。
- ITileSquareText02 squareContent = TileContentFactory.CreateTileSquareText02();
- squareContent.TextHeading.Text = key;
- squareContent.TextBodyWrap.Text = array[0];
- //創(chuàng)建寬模板,ITileWideSmallImageAndText02左側(cè)是一個(gè)較小的圖像,右側(cè)上面是第一行上較大文本的標(biāo)題字符串,下面是四行四個(gè)常規(guī)文本的字符串。文本不自動(dòng)換行。
- 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]; //第一行數(shù)據(jù)
- break;
- case 1:
- tileContent.TextBody2.Text = array[i]; //第二行數(shù)據(jù)
- break;
- case 2:
- tileContent.TextBody3.Text = array[i]; //第三行數(shù)據(jù)
- break;
- case 3:
- tileContent.TextBody4.Text = array[i]; //第四行數(shù)據(jù)
- break;
- }
- }
- tileContent.SquareContent = squareContent;
- TileUpdateManager.CreateTileUpdaterForApplication().Update(tileContent.CreateNotification());
- TileUpdateManager.CreateTileUpdaterForApplication().EnableNotificationQueue(true);
- }
- }
- catch
- {
- }
- }
注意:動(dòng)態(tài)磁貼必須要在實(shí)體機(jī)器上,在模擬器中是無法顯示動(dòng)態(tài)磁貼的效果。
2.輔助磁貼(二級(jí)磁貼)
輔助磁貼使用戶能夠?qū)?Windows 應(yīng)用商店應(yīng)用的特定內(nèi)容和深層鏈接—對(duì)固定應(yīng)用內(nèi)部一個(gè)特定位置的引用—發(fā)送到“開始”屏幕上。輔助磁貼使用戶能夠使用好友、新聞源、股票報(bào)價(jià)和其他對(duì)其很重要的內(nèi)容來個(gè)性化“開始”屏幕體驗(yàn)。創(chuàng)建輔助磁貼的選項(xiàng)最常在 UI 中顯示為“附到開始菜單”選項(xiàng)。固定內(nèi)容也就是為它創(chuàng)建輔助磁貼。此選項(xiàng)常常顯示為應(yīng)用欄上的一個(gè)標(biāo)志符號(hào)。通過觸摸或單擊來選擇輔助磁貼,會(huì)啟動(dòng)到父應(yīng)用,以突出一種以固定內(nèi)容或聯(lián)系人為中心的體驗(yàn)。只有用戶才可以固定輔助磁貼;應(yīng)用不能在未獲得用戶許可的情況下以編程方式固定輔助磁貼。用戶還可以通過“開始”屏幕或通過父應(yīng)用,對(duì)輔助磁貼進(jìn)行顯式刪除控制。
1).在固定輔助磁貼前,用戶必須確認(rèn),要求對(duì)此進(jìn)行確認(rèn)的彈出窗口應(yīng)當(dāng)顯示在調(diào)用固定請(qǐng)求的按鈕旁邊。
- 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));
- }
2).添加輔助磁貼。首先需要設(shè)置輔助磁貼的ID
- public const string appbarTileId = "SecondaryTile.AppBar";
- private async void AddButtonClicked(object sender, RoutedEventArgs e)
- {
- //當(dāng)用戶選擇應(yīng)用欄上的按鈕時(shí),會(huì)顯示一個(gè)要求用戶進(jìn)行確認(rèn)的彈出窗口。
- //若要確保在顯示彈出窗口時(shí)不取消應(yīng)用欄,必須設(shè)置應(yīng)用欄的 IsSticky 屬性。
- this.BottomAppBar.IsSticky = true;
- if(SecondaryTile.Exists(appbarTileId))
- {
- //取消相應(yīng)的輔助磁貼
- SecondaryTile secondaryTile = new SecondaryTile(appbarTileId);
- bool isUnpinned = await secondaryTile.RequestDeleteForSelectionAsync(GetElementRect((FrameworkElement)sender), Windows.UI.Popups.Placement.Above);
- ToggleAppBarButton(isUnpinned);
- }
- else
- {
- //輔助磁貼的一些屬性需要設(shè)置后才能固定輔助磁貼.
- //•磁貼的唯一 ID
- //•短名稱
- //•顯示名稱
- //•磁貼選項(xiàng)
- //•徽標(biāo)圖像
- //•激活輔助磁貼時(shí)將傳遞到父應(yīng)用程序的參數(shù)字符串
- 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);
- //指定前景文本顏色和小徽標(biāo)。
- secondaryTile.ForegroundText = ForegroundText.Dark;
- secondaryTile.SmallLogo = new Uri("ms-appx:///Assets/small.jpg");
- //調(diào)用異步方法將輔助磁貼固定。
- //實(shí)際上這種方法不是將磁貼直接固定到“開始”屏幕,而是會(huì)顯示一個(gè)要求用戶允許這樣做的確認(rèn)提示框。
- bool isPinned = await secondaryTile.RequestCreateForSelectionAsync(GetElementRect((FrameworkElement)sender), Windows.UI.Popups.Placement.Above);
- ToggleAppBarButton(!isPinned);
- }
- this.BottomAppBar.IsSticky = false;
- }
以上就完成了,輔助磁貼的添加和顯示。
原文鏈接:http://www.cnblogs.com/akwwl/archive/2013/02/18/2880120.html
【編輯推薦】






