打算把 WP7 應用升級的朋友,先看看 WP8 有哪些「投入小、加分大」的 Key Feature。這篇挑了兩個最值得先做的。
升級到 WP8 必你如果知道的 13 個特性系列目錄:https://www.linuxidc.com/Linux/2013-08/89003.htm
WP7 時代,應用被切到後台後,只有按 Back 鍵或某些 FAS 情境能回到原狀態;從應用列表或 Tile 啟動,舊實例會被終止並建立新實例。WP8 解決了這件事,但不是預設開啟,需要開發者做一點調整。
啟用方式很簡單,用右鍵編輯 manifest,在 Tasks 下的 DefaultTask 節點加上 ActivationPolicy="Resume":
<Tasks> <DefaultTask Name="_default" NavigationPage="MainPage.xaml" ActivationPolicy="Resume"/> </Tasks>
接著可以依啟動來源做更細的處理,例如 Tile、應用列表與 Deep Link 各自導向不同頁面。判斷的時機是在 Activated 與 Navigating 事件中:
// 應用被喚到前景時執行(首次啟動時不會執行)
private void Application_Activated(object sender, ActivatedEventArgs e)
{
mustClearPagestack = CheckDeactivationTimeStamp();
}
void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e)
{
// e.NavigationMode == NavigationMode.New
}
依應用需求自行適配即可。我實測下 BackStack 的記錄能保存得還不錯。MSDN 參考:https://msdn.microsoft.com/en-us/library/windowsphone/develop/jj735579(v=vs.105).aspx
WP8 的 Tile 有 small、medium、wide 三種尺寸,wide 是這次新增的,可以佔據整個一列,視覺效果很不錯。模板則有三種:Flip、Iconic、Cycle。
Flip Tile 與 WP7 的翻轉效果相似,圖片與文字互翻,同樣支援推送與後台更新。
FlipTileData TileData = new FlipTileData()
{
Title = "[title]",
BackTitle = "[back of Tile title]",
BackContent = "[back of medium Tile size content]",
WideBackContent = "[back of wide Tile size content]",
Count = [count],
SmallBackgroundImage = [small Tile size URI],
BackgroundImage = [front of medium Tile size URI],
BackBackgroundImage = [back of medium Tile size URI],
WideBackgroundImage = [front of wide Tile size URI],
WideBackBackgroundImage = [back of wide Tile size URI],
};
對應的 XAML 推送範本:
<?xml version="1.0" encoding="utf-8"?>
<wp:Notification xmlns:wp="WPNotification" Version="2.0">
<wp:Tile Id="[Tile ID]" Template="FlipTile">
<wp:SmallBackgroundImage [Action="Clear"]>[small Tile size URI]</wp:SmallBackgroundImage>
<wp:WideBackgroundImage Action="Clear">[front of wide Tile size URI]</wp:WideBackgroundImage>
<wp:WideBackBackgroundImage Action="Clear">[back of wide Tile size URI]</wp:WideBackBackgroundImage>
<wp:WideBackContent Action="Clear">[back of wide Tile size content]</wp:WideBackContent>
<wp:BackgroundImage Action="Clear">[front of medium Tile size URI]</wp:BackgroundImage>
<wp:Count Action="Clear">[count]</wp:Count>
<wp:Title Action="Clear">[title]</wp:Title>
<wp:BackBackgroundImage Action="Clear">[back of medium Tile size URI]</wp:BackBackgroundImage>
<wp:BackTitle Action="Clear">[back of Tile title]</wp:BackTitle>
<wp:BackContent Action="Clear">[back of medium Tile size content]</wp:BackContent>
</wp:Tile>
</wp:Notification>
Iconic Tile 是 WP8 獨有的,多見於系統內建應用如郵件、短信,用圖標加三行數字呈現資訊:
<?xml version="1.0" encoding="utf-8"?>
<wp:Notification xmlns:wp="WPNotification" Version="2.0">
<wp:Tile Id="[Tile ID]" Template="IconicTile">
<wp:SmallIconImage [Action="Clear"]>[small Tile size URI]</wp:SmallIconImage>
<wp:IconImage Action="Clear">[medium/wide Tile size URI]</wp:IconImage>
<wp:WideContent1 Action="Clear">[1st row of content]</wp:WideContent1>
<wp:WideContent2 Action="Clear">[2nd row of content]</wp:WideContent2>
<wp:WideContent3 Action="Clear">[3rd row of content]</wp:WideContent3>
<wp:Count Action="Clear">[count]</wp:Count>
<wp:Title Action="Clear">[title]</wp:Title>
<wp:BackgroundColor Action="Clear">[hex ARGB format color]</wp:BackgroundColor>
</wp:Tile>
</wp:Notification>
C# 建立方式:
IconicTileData TileData = new IconicTileData()
{
Title = "[title]",
Count = [count],
WideContent1 = "[1st row of content]",
WideContent2 = "[2nd row of content]",
WideContent3 = "[3rd row of content]",
SmallIconImage = [small Tile size URI],
IconImage = [medium/wide Tile size URI],
BackgroundColor = [.NET color type of Tile]
};
提醒:更新 Tile 前若應用已不存在該次更新的資訊,記得用 Action="Clear" 或空值清掉舊內容,否則使用者會看到過期數據。