20、磁贴和磁贴通知(tile)(上)
?
????? win8 中的磁鐵操作要比 windows phone 中的復雜一些,不過原理大同小異,都是一些固定內容模型。
????? 參考文檔:?
???????????????? 磁貼和磁貼通知 :?http://msdn.microsoft.com/zh-cn/library/windows/apps/hh779724.aspx
??????????????? ?磁貼模板目錄 :??? http://msdn.microsoft.com/zh-cn/library/windows/apps/hh761491.aspx
?
????? Tiles 在系統的開始菜單中代表著應用,并且應用可以使用 tile 和 badge 通知來向用戶顯示 新的、重大的、定制的內容。
??????Tile 通知是一種固定格式的 XML,其中包含文字和圖片內容。在應用程序清單中,必須包含一張正方形的的 logo,如果
?應用程序也想使用寬模版 logo,也需要在清單中注明。如果你的應用中同樣支持寬 tile,強烈建議你預加載 方形和寬形 在預加
?載的 xml 中,從而無論開始菜單中的 tile 是方形或者 寬形的 都可以接收通知。
??????? 因為通知(notifications) 僅僅是一些 XML,你可以通過很多種方法進行創建。可以使用本示例中包含的 NotificationsExtensions 類庫,也可以使用一些字符串。
位于 Windows.UI.Notifications 命名空間下的 TileUpdateManager 類:
// 創建用于更改和更新啟動菜單圖塊的 TileUpdater 對象。此類還提供對系統提供的平鋪模板 XML 內容的訪問,以便您可以自定義用于更新您平鋪的內容。public static class TileUpdateManager{// 創建并初始化 TileUpdater 的新實例,此操作可讓您更改調用應用程序圖塊的外觀。// 返回結果:// 用于將更改發送到應用程序的平鋪的對象。public static TileUpdater CreateTileUpdaterForApplication();// 創建并初始化圖塊 TileUpdater 的新實例,該圖塊屬于和調用應用程序位于同一包中的另一應用程序。TileUpdater 允許開發人員更改該平鋪外觀。// applicationId:// 標題的唯一 ID。// 返回結果:// 用于通過 applicationId 將更改發送到平鋪標識的對象。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);}?
?
?
1、Send tile notification with text:
??? NotificationsExtensions 類庫同樣的包含在了源代碼文件中,所有源代碼文件在 前言 中可以下載。
?
//使用 NotificationsExtensions 類庫 <Button x:Name="UpdateTileWithText" Content="Send tile notification with text" Click="UpdateTileWithText_Click"/>//使用模版自定義字的符串<Button x:Name="UpdateTileWithTextWithStringManipulation" Content="Send tile notification created with strings" Click="UpdateTileWithTextWithStringManipulation_Click"/>//清除 tile <Button x:Name="ClearTile" Content="Clear tile" Click="ClearTile_Click"/>
三個按鈕相應的單擊事件:
ITileWideText03 tileContent = TileContentFactory.CreateTileWideText03();tileContent.TextHeadingWrap.Text = "Hello World! My very own tile notification";//因為用戶可能把桌面上的寬 tile 設置為 正方形的,所以在這里 //把 方形的 模版附加到 寬形的模版上面ITileSquareText04 squareContent = TileContentFactory.CreateTileSquareText04();squareContent.TextBodyWrap.Text = "Hello World! My very own tile notification";tileContent.SquareContent = squareContent;//發送這個 notification TileUpdateManager.CreateTileUpdaterForApplication().Update(tileContent.CreateNotification());}
?
//使用自定義的 xml private void UpdateTileWithTextWithStringManipulation_Click(object sender, RoutedEventArgs e){// create a string with the tile template xmlstring tileXmlString = "<tile>"+ "<visual>"+ "<binding template='TileWideText03'>"+ "<text id='1'>Hello World! My very own tile notification</text>"+ "</binding>"+ "<binding template='TileSquareText04'>"+ "<text id='1'>Hello World! My very own tile notification</text>"+ "</binding>"+ "</visual>"+ "</tile>";// 創建一個 DOM 對象Windows.Data.Xml.Dom.XmlDocument tileDOM = new Windows.Data.Xml.Dom.XmlDocument();// 加載 xml 字符串到 DOM ,捕獲任何無效的 xml 字符tileDOM.LoadXml(tileXmlString);// 創建一個 tile notificationTileNotification tile = new TileNotification(tileDOM);// 發送這個 notification 到這個應用的 application tile
TileUpdateManager.CreateTileUpdaterForApplication().Update(tile);
//返回 XML 表示形式及其所有子代。string xmlStr = tileDOM.GetXml();}
?
// 清除桌面通知 private void ClearTile_Click(object sender, RoutedEventArgs e){// 刪除所有更新并將平鋪還原為其默認內容(如應用程序清單所聲明)。 TileUpdateManager.CreateTileUpdaterForApplication().Clear(); }
桌面顯示的寬屏通知:
?
?
2、Send tile notification with local images :
???? Tile notifications 可以指定本地的圖片。這些圖片可以引用應用程序包 (使用 ms-appx:///? ?協議),或者引用本地的
存儲空間(local storage)(程序運行時下載的圖片)使用 (ms-appdata:///local/?? 協議)。圖片的格式必須是 .png,
.jpg, .jpeg 或者 gif。圖片的最大為 200 KB, 尺寸最大為 1024 x 1024。
//使用 NotificationsExtensions 類庫<Button x:Name="UpdateTileWithImage" Content="Send tile notification with local images" Click="UpdateTileWithImage_Click"/>//使用模版自定義字的符串 <Button x:Name="UpdateTileWithImageWithStringManipulation" Content="Send tile notification created with strings" Click="UpdateTileWithImageWithStringManipulation_Click"/>//清除 tile<Button x:Name="ClearTile" Content="Clear tile" Click="ClearTile_Click"/>?
?
//使用 NotificationsExtensions 類庫void UpdateTileWithImage_Click(object sender, RoutedEventArgs e){ITileWideImageAndText01 tileContent = TileContentFactory.CreateTileWideImageAndText01();tileContent.TextCaptionWrap.Text = "This tile notification uses ms-appx images"; //tile 上的圖片tileContent.Image.Src = "ms-appx:///images/redWide.png";
//圖片的描述文字 tileContent.Image.Alt = "Red image";ITileSquareImage squareContent = TileContentFactory.CreateTileSquareImage();squareContent.Image.Src = "ms-appx:///images/graySquare.png";squareContent.Image.Alt = "Gray image";tileContent.SquareContent = squareContent;TileUpdateManager.CreateTileUpdaterForApplication().Update(tileContent.CreateNotification());}
?
//使用模版自定義字的符串void UpdateTileWithImageWithStringManipulation_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);}?
//清除 tile private void ClearTile_Click(object sender, RoutedEventArgs e){TileUpdateManager.CreateTileUpdaterForApplication().Clear();}
顯示效果(上面為一張紅色的 png 圖片):
?
?
3、Send tile notification with web images :
???? 本示例講述如果使用 web 服務中的圖片。
???? Tile notifications 也可以指定 web 服務端的圖片。這些圖片可以通過使用? http://? 或者?https://?? 協議進行傳輸。圖片的格式必須是 .png,
.jpg, .jpeg 或者 gif。圖片的最大為 200 KB, 尺寸最大為 1024 x 1024。
//圖片的 uri 地址 <TextBox x:Name="ImageUrl" Text="http://" />?
//使用 NotificationsExtensions 類庫 <Button x:Name="UpdateTileWithWebImage" Content="Send tile notification with web images" Click="UpdateTileWithWebImage_Click" /> //使用模版自定義字的符串<Button x:Name="UpdateTileWithWebImageWithStringManipulation" Content="Send tile notification created with strings"Click="UpdateTileWithWebImageWithStringManipulation_Click"/> //清除 tile<Button x:Name="ClearTile" Content="Clear tile" Click="ClearTile_Click"/>
?
//使用 NotificationsExtensions 類庫 void UpdateTileWithWebImage_Click(object sender, RoutedEventArgs e){ITileWideImageAndText01 tileContent = TileContentFactory.CreateTileWideImageAndText01();tileContent.TextCaptionWrap.Text = "This tile notification uses web images.";//重要: //在工程清單中,Internet (Client) 功能必須選中(在 Capabilities 選擇卡中),從而 //可以顯示服務器端的圖片(通過 http:// 或者 https:// 協議)tileContent.Image.Src = ImageUrl.Text;tileContent.Image.Alt = "Web image";ITileSquareImage squareContent = TileContentFactory.CreateTileSquareImage();squareContent.Image.Src = ImageUrl.Text;squareContent.Image.Alt = "Web image";tileContent.SquareContent = squareContent;TileUpdateManager.CreateTileUpdaterForApplication().Update(tileContent.CreateNotification());}
?
?4、Send badge notification :
??? 本示例是講述如何發送 badge 通知。
????Badge notifications 使用一個數字 或者 系統提供的圖形 來更新你的應用的 tile。Badges 通知可以同時在 方形或者 寬形
?的 tile 上面顯示。
????? 三個按鈕:
?
?? 更新接收更新,輸入的數字 的文本框:
??? 顯示結果:
?
?
?
更新接收更新,選擇輸入的圖案 的下拉框:
顯示結果:
?????
? 前端的 xaml :
// 用 數字 更新 tile<TextBox x:Name="NumberInput" Text="1" />// 用 圖案 更新 tile <ComboBox x:Name="GlyphList"><ComboBoxItem x:Name="none">none</ComboBoxItem><ComboBoxItem x:Name="activity">activity</ComboBoxItem><ComboBoxItem x:Name="alert">alert</ComboBoxItem><ComboBoxItem x:Name="available">available</ComboBoxItem><ComboBoxItem x:Name="away">away</ComboBoxItem><ComboBoxItem x:Name="busy">busy</ComboBoxItem><ComboBoxItem x:Name="newMessage">newMessage</ComboBoxItem><ComboBoxItem x:Name="paused">paused</ComboBoxItem><ComboBoxItem x:Name="playing">playing</ComboBoxItem><ComboBoxItem x:Name="unavailable">unavailable</ComboBoxItem><ComboBoxItem x:Name="error">error</ComboBoxItem><ComboBoxItem x:Name="attention">attention</ComboBoxItem></ComboBox>//使用 NotificationsExtensions 類庫 <Button x:Name="UpdateBadge" Content="Send badge notification" Click="UpdateBadge_Click" />//使用模版自定義字的符串<Button x:Name="UpdateBadgeWithStringManipulation" Content="Send badge notification created with strings" Click="UpdateBadge_Click" /> //清除 tile <Button x:Name="ClearBadge" Content="Clear badge" Click="ClearBadge_Click" />?
?
1)用數字更新 :
?//使用 NotificationsExtensions 類庫void UpdateBadge_Click(object sender, RoutedEventArgs e){int number;if (Int32.TryParse(NumberInput.Text, out number)){//使用 NotificationsExtensions 類庫BadgeNumericNotificationContent badgeContent =
new BadgeNumericNotificationContent((uint)number);// 用 數字 更新 tile
BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badgeContent.CreateNotification());
}else{//用戶輸入的不是數字 不會更新 }}
?
//使用模版自定義字的符串 void UpdateBadge_Click(object sender, RoutedEventArgs e){ int number;if (Int32.TryParse(NumberInput.Text, out number)){// 用 badge 的 xml 模版創建字符串
string badgeXmlString = "<badge value='" + number + "'/>";Windows.Data.Xml.Dom.XmlDocument badgeDOM = new Windows.Data.Xml.Dom.XmlDocument();try{// 創建 DOM badgeDOM.LoadXml(badgeXmlString);// 定義圖塊徽標覆蓋的更新的內容、關聯元數據和事件以及過期時間。 //徽章可以顯示從 1 到 99 的數字或 狀態標志符號。BadgeNotification badge = new BadgeNotification(badgeDOM);
// CreateBadgeUpdaterForApplication() : 創建并初始化 BadgeUpdater 的新實例,此操作可讓您更改調用應用程序圖塊上的徽章外觀或內容。 BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badge);
// 返回 XML 表示形式及其所有子代。string strXML = badgeDOM.GetXml();}catch (Exception){//輸入的不是有效的 badge 的 xml 模版 }}else{// 用戶輸入的不是數字 不會更新 }}
?
//清除 badgevoid ClearBadge_Click(object sender, RoutedEventArgs e){BadgeUpdateManager.CreateBadgeUpdaterForApplication().Clear();}2) 用符號更新 badge :
//使用 NotificationsExtensions 類庫void UpdateBadge_Click(object sender, RoutedEventArgs e){//注意:這通常會創建新的BadgeGlyphNotificationContent(GlyphValue.Alert)或任何GlyphValue的值(GlyphValue.Alert) or any of the values of GlyphValueBadgeGlyphNotificationContent badgeContent = new BadgeGlyphNotificationContent((GlyphValue)GlyphList.SelectedIndex);BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badgeContent.CreateNotification());}?
?
//使用模版自定義字的符串 void UpdateBadge_Click(object sender, RoutedEventArgs e){// 用 badge 的 xml 模版創建字符串string badgeXmlString = "<badge value='" + ((ComboBoxItem)GlyphList.SelectedItem).Content.ToString() + "'/>";Windows.Data.Xml.Dom.XmlDocument badgeDOM = new Windows.Data.Xml.Dom.XmlDocument();try{// 創建 DOM badgeDOM.LoadXml(badgeXmlString);//把 xml 字符串 加載到 xml 中,并且捕捉非法的 xml 字符BadgeNotification badge = new BadgeNotification(badgeDOM);BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badge);}catch (Exception){// 輸入的不是有效的 badge 的 xml 模版 }}?
注: badge 中圖形的值 :
// 在 NotificationsExtensions 的擴展庫中/// <summary>/// The types of glyphs that can be placed on a badge./// </summary>public enum GlyphValue{/// <summary>/// No glyph. If there is a numeric badge, or a glyph currently on the badge,/// it will be removed./// </summary>None = 0,/// <summary>/// A glyph representing application activity./// </summary> Activity,/// <summary>/// A glyph representing an alert./// </summary> Alert,/// <summary>/// A glyph representing availability status./// </summary> Available,/// <summary>/// A glyph representing away status/// </summary> Away,/// <summary>/// A glyph representing busy status./// </summary> Busy,/// <summary>/// A glyph representing that a new message is available./// </summary> NewMessage,/// <summary>/// A glyph representing that media is paused./// </summary> Paused,/// <summary>/// A glyph representing that media is playing./// </summary> Playing,/// <summary>/// A glyph representing unavailable status./// </summary> Unavailable,/// <summary>/// A glyph representing an error./// </summary> Error,/// <summary>/// A glyph representing attention status./// </summary> Attention}?
轉載于:https://www.cnblogs.com/hebeiDGL/archive/2012/09/20/2695573.html
總結
以上是生活随笔為你收集整理的20、磁贴和磁贴通知(tile)(上)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 积累一点小Sql 表的纵横互转玩玩
- 下一篇: INV标准报表+INVARAAS.rdf