Windows8应用生命周期 Metro Style Apps Lifecycle
1. Lifecycle
Windows8應用的生命周期,與Windows Phone及其他手機平臺相比更加簡單,Windows8應用只有:未運行、運行、掛起,三個狀態。其狀態如下圖:
當應用切換到后臺,或者系統電量低時,應用會被掛起,掛起后的應用還駐留在內存中,頁面的導航狀態,以及當前頁面的內容等一切內容都在。當用戶切換回應用時,系統自動恢復到掛起前的狀態,我們不需要做任何工作,唯一需要的可能是要刷新一下數據,比如基于Location的應用和新聞類的應用,因為在掛起的這段時間里,數據可能已經發生了變化。
系統會終止掛起的應用,以節省內存和電力。這時應用就從內存里消失了。如果用戶再次切換回應用里,就要重新啟動了。Windows8應用不應該主動退出,也就是不應該提供退出操作。不過,用戶可以主動地關掉一個應用,如使用關閉手勢,Alt+F4終止應用。在使用關閉手勢和Alt+F4后,應用會被掛起10秒鐘,然后被終止。
好的應用,應該是,在不是被用戶主動關閉(Alt+F4,關閉手勢)的情況下,當應用再次被啟動時,應恢復到掛起前的狀態,以給用戶應用始終都在運行的感覺。這就需要我們在應用被掛起時,應該保存應用的狀態數據。以便在重新啟動時恢復界面。
2. 應用數據的保存和恢復
應用的數據可會為應用數據(app data)和會話數據(session data),應用數據對用戶來說是持久的,在多個會話中保持一致的,比如用戶的設置信息等。而會話數據是臨時的,只在本次會話中有效的數據。Windows8的建議是,應用數據應在發生改變時立即保存,而會話數據則在應用掛起時保存,在應用重新啟動時恢復。
Windows提供了?Windows.Storage.ApplicationData,這個對象分別提供了LocalFolder、LocalSettings和RoamingFolder、RoamingSettings,2組4個屬性對象,來分別進行本地和漫游的應用數據保存,如果把數據保存到漫游的對象里,則Windows會自動在用戶的不同設備間同步數據。
3. 會話數據的保存和恢復
會話數據通過SuspensionManager和LayoutAwarePage,來進行保存和恢復。在添加了BasicPage模版頁到工程后,VS2012會自動添加這兩個類到工程的Common目錄里。
3.1 SuspensionManager
SuspensionManager負責對會話數據的管理,使用它可以在應用啟動時恢復Frame的導航狀態和頁面的狀態,在應用掛起時保存這些數據.
sealed partial class App : Application{public App(){this.InitializeComponent();// 在應用掛起時,獲得響應this.Suspending += OnSuspending;}protected async override void OnLaunched(LaunchActivatedEventArgs args){Frame rootFrame = Window.Current.Content as Frame;// Do not repeat app initialization when the Window already has content,// just ensure that the window is activeif (rootFrame == null){// Create a Frame to act as the navigation context and navigate to the first pagerootFrame = new Frame();// 注冊我們要保存和恢復的FrameHelloWorld.Common.SuspensionManager.RegisterFrame(rootFrame, "appFrame");if (args.PreviousExecutionState == ApplicationExecutionState.Terminated){//當狀態是Terminated時,恢復應用的會話數據await HelloWorld.Common.SuspensionManager.RestoreAsync();}// Place the frame in the current WindowWindow.Current.Content = rootFrame;}if (rootFrame.Content == null){if (!rootFrame.Navigate(typeof(MainPage), args.Arguments)){throw new Exception("Failed to create initial page");}}// Ensure the current window is active Window.Current.Activate();}private async void OnSuspending(object sender, SuspendingEventArgs e){var deferral = e.SuspendingOperation.GetDeferral();// 應用要被掛起了,保存應用的狀態,并退出后臺的操作,打開的文件等await HelloWorld.Common.SuspensionManager.SaveAsync();deferral.Complete();}在啟動時,并不是所有的情況都應該恢復數據,代碼中判斷了只有在args.PreviousExecutionState是Terminated狀態時才會調用RestoreAsync。PreviousExecutionState是這次啟動之前程序的狀態,是一個ApplicationExecutionState類型的枚舉值,一共有5種狀態。只有Terminated是應用由系統在掛起并終止的狀態,需要恢復會話數據,其它情況下的終止,像重啟、重新安裝、用戶主動關閉等,我們通常認為是會話已經結束,重新啟動新的會話即可,不需要恢復。
| NotRunning | The user first activates the app after:
The user closes the app through the close gesture or Alt+F4 and activates it within about 10 seconds of closing it. | Display its initial UI and perform initialization tasks. |
| Running | The app is activated through a secondary tile or one of the?activation contracts and extensions?while it is running. | Respond to the activation event as appropriate. |
| Suspended | The app is activated through a secondary tile or one of the?activation contracts and extensions?while Windows is suspending it or after Windows has suspended it. | Respond to the activation event as appropriate. |
| Terminated | Windows successfully suspends the app and then it is terminated. For example, Windows can terminate a suspended app if the system is running low on resources. Some apps, such as games, can be pretty resource intensive. | Restore itself to the way the user left it, rather than starting fresh. Use data saved during app suspension. Refresh content or network connections that may have become stale. |
| ClosedByUser | The user closes the app through the close gesture or Alt+F4 and takes longer than 10 seconds to activate the app again. | Display its initial UI and perform initialization tasks, rather than restoring its previous state. |
3.2?LayoutAwarePage
LayoutAwarePage是Page的一個派生類,它提供了兩個方法SaveState和LoadState,分別用于保存和恢復會話數據
protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState){// 從pageState中,讀取保存的數據,恢復到界面上 }protected override void SaveState(Dictionary<String, Object> pageState){// 將需要保存的數據,保存到pageState}更詳細內容可以參考這里:http://msdn.microsoft.com/en-us/library/windows/apps/hh986968.aspx
轉載于:https://www.cnblogs.com/E7868A/archive/2012/11/18/2775912.html
總結
以上是生活随笔為你收集整理的Windows8应用生命周期 Metro Style Apps Lifecycle的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Daily Scrum M2 11-14
- 下一篇: UISeatchBar