Windows 8 開發之動態磚和次要磚(二級磁貼)

來源:互聯網
上載者:User

  磁貼在系統的開始菜單中代表著應用,動態磚可以向使用者顯示新的、重大的、定製的內容。 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);        }

 

  另外我們可以下載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            {             }    }


注意:動態磚必須要在實體機器上,在模擬器中是無法顯示動態磚的效果。

  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));   } 

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

  以上就完成了,次要磚的添加和顯示。

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.