一般我們在開發Windows Phone App時,很常會將動態磚定選到開始畫面上,但是日積月累我們寫的每支App需要創建動態磚時,都需要撰寫一次新增動態專的程式碼,如此一來變得相當麻煩,為何不自定義類別,或者封裝成Dll後給其他應用程式加入參考呢?!
本篇文章將引導您自定義Flip Tile 翻轉動態磚 釘選 更新 刪除 類別(Class),
一般我們在開發Windows Phone App時,很常會將動態磚定選到開始畫面上,但是日積月累我們寫的每支App需要創建動態磚時,都需要撰寫一次新增動態專的程式碼,如此一來變得相當麻煩,為何不自定義類別,或者封裝成Dll後給其他應用程式加入參考呢?!
本篇文章將引導您自定義Flip Tile 翻轉動態磚 釘選 更新 刪除 類別(Class),
本篇文章目的清楚明瞭,直接進入Code,說明一併在程式碼內附上了!
1: public class TileController
2: {
3: //檢查動態磚是否有被釘選
4: public static Boolean IsOnDesktop(string TileName)
5: {
6: //根據名稱查詢動態磚
7: ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains(TileName));
8: return (TileToFind != null);
9: }
10: //更新動態磚
11: public static void UpdataTile(string TileName, int Count, Uri Background, string Title, Uri BackBackground, string Back_Title,string BackContent ,string XamlName)
12: {
13: ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains(TileName));
14:
15: //判斷是否存在
16: if (TileToFind != null)
17: {
18: //創建動態磚
19: StandardTileData NewTileData = new StandardTileData
20: {
21: //正面背景圖
22: BackgroundImage = Background,
23: //正面標題
24: Title = Title,
25: //計數
26: Count = Count,
27: //背面標題
28: BackTitle = Back_Title,
29: //背面內容文字
30: BackContent = BackContent,
31: //背面背景
32: BackBackgroundImage = BackBackground
33: };
34: try
35: {
36: //更新動態磚
37: TileToFind.Update(NewTileData);
38: }
39: catch
40: {
41: Debug.WriteLine("Tile更新失敗");
42: }
43: }
44: }
45:
46: public static void Pin(string TileName, int Count, Uri Background, string Title, Uri BackBackground, string Back_Title, string BackContent, string XamlName)
47: {
48: ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains(TileName));
49: //判斷是否存在
50: if (TileToFind == null)
51: {
52: //創建動態磚
53: StandardTileData NewTileData = new StandardTileData
54: {
55: //正面背景圖
56: BackgroundImage = Background,
57: //正面標題
58: Title = Title,
59: //計數
60: Count = Count,
61: //背面標題
62: BackTitle = Back_Title,
63: //背面內容文字
64: BackContent = BackContent,
65: //背面背景
66: BackBackgroundImage = BackBackground
67: };
68:
69: try
70: {
71: //創建動態磚
72: ShellTile.Create(new Uri("/"+XamlName, UriKind.Relative), NewTileData);
73:
74: }
75: catch {
76: //創建失敗
77: }
78: }
79: }
80: //刪除動態專
81: public static void UnPin(string TileName)
82: {
83: //尋找動態磚
84: ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains(TileName));
85: //如果動態磚存在在開始畫面,則刪除動態磚。
86: if (TileToFind != null)
87: {
88: TileToFind.Delete();
89: }
90: }
91: }
用法 :
1: //判斷動態磚
2: if (TileController.IsOnDesktop(TileName))
3: {
4: //動態磚已存在
5: }
6: else {
7: //動態磚不存在
8: }
9:
10:
11: //創建動態磚
12: TileController.Pin(
13: TileName,//動態唯一識別
14: 0,//季數數字
15: new Uri("/Assets/ApplicationIcon.png", UriKind.Relative),//正面底圖
16: "正面標題",
17: new Uri("/Assets/ApplicationIcon.png", UriKind.Relative),//背面底圖
18: "背面標題",
19: "背面內文",
20: "PageB.xaml?id=" + TileName //頁面資訊+參數
21: );
22:
23:
24: //更新動態磚
25: TileController.UpdataTile(
26: TileName,//動態唯一識別
27: 0,//季數數字
28: new Uri("/Assets/ApplicationIcon.png", UriKind.Relative),//正面底圖
29: "正面標題",
30: new Uri("/Assets/ApplicationIcon.png", UriKind.Relative),//背面底圖
31: "背面標題",
32: "背面內文",
33: "PageB.xaml?id=" + TileName //頁面資訊+參數
34: );
35:
36:
37: //刪除動態專
38: TileController.UnPin(TileName);
如此一來我們就可以利用自定義類別來控制Flip Tile 的翻轉動態磚囉!
References :
[筆記] C# Windows Phone 8 WP8,解決再ScheduledAgent中無法使用【ShellTile.ActiveTiles.First()】檢查動態磚。
C# Windows Phone 8 WP8,自定義FlipTile動態磚樣式,用Xaml創建動態磚樣式!
[小技巧] Windows Phone 8 WP8 開發,讓APP的底色與系統動態磚的配色同步 !
文章中的敘述如有觀念不正確錯誤的部分,歡迎告知指正 謝謝
轉載請註明出處,並且附上本篇文章網址 ! 感謝。
SUKI