Windows 8 應用通常涉及到兩種資料類型:應用資料與會話資料。在上一篇提到的本機資料儲存就是應用程式層面的資料,包括應用參數設定、使用者重要資料等。那麼會話層面的資料是基於使用者每次使用應用而形成,這些資料可能不需要留存在裝置中。在整個應用生命週期中,應用啟動後便進入運行狀態。當使用者離開或系統進入待機狀態時,應用會進入掛起狀態,此時應用將被放入到記憶體中,待使用者重新使用時便會恢複成運行狀態。
在這個過程中使用者之前可能已經錄入了一些資料,並且希望在應用恢複時可以繼續進行錄入。對於開發人員來說,我們需要在應用掛起時將一些會話資料進行儲存,當應用恢複後同時將暫存資料複原,以便讓使用者繼續使用。需要注意的是MSDN中提到:“當使用者通過按 Alt+F4 或使用關閉手勢關閉應用時,應用將被掛起 10 秒鐘然後被終止。”也就意味著關閉的應用只有10秒鐘時間可以被恢複。下面將通過執行個體進行示範,首先建立一個Textbox 讓使用者錄入名字進行會話操作。我們首先來嘗試一下沒有進行掛起暫存處理的應用是何種結果。
<StackPanel Grid.Row="1" Margin="120,30,0,0"> <StackPanel Orientation="Horizontal" Margin="0,20,0,20"> <TextBlock Text="Name: " Style="{StaticResource BasicTextStyle}" Width="50"/> <TextBox x:Name="nameInput" Width="200"/> </StackPanel></StackPanel>掛起
直接按F5運行應用,在Name 欄中輸入名字或任一字元。在VS2012的Debug Location 工具列可以看到掛起(Suspend )的選項,我們選擇掛起並終止(Suspend and shutdown),程式掛起後從系統左側功能表列裡找到之前的應用重新啟用,恢複後的應用Name 欄中的文字已經丟失。對於名字這樣的簡單錄入還可以接受,如果錄入項較多的話那將損失慘重。
接下來我們將進行應用掛起處理,開啟App.xaml.cs 程式,在OnLaunched 方法中建立了rootFrame,當rootFrame 為Null 時將重新建立Frame,在這個邏輯判斷中要使用SuspensionManager.RegisterFrame 方法進行rootFrame 註冊,這樣才可以使應用獲得根Frame 資訊並進行資料存放區。
if (rootFrame == null){ // Create a Frame to act as the navigation context and navigate to the first page rootFrame = new Frame(); SuspensionDemo.Common.SuspensionManager.RegisterFrame(rootFrame, "appFrame"); if (args.PreviousExecutionState == ApplicationExecutionState.Terminated) { //TODO: Load state from previously suspended application } // Place the frame in the current Window Window.Current.Content = rootFrame;}
在OnSuspending 方法中,使用SuspensionManager.SaveAsync 方法將掛起應用的目前狀態進行儲存,這裡可以調用非同步作業來進行處理。
private async void OnSuspending(object sender, SuspendingEventArgs e){ var deferral = e.SuspendingOperation.GetDeferral(); //TODO: Save application state and stop any background activity await SuspensionDemo.Common.SuspensionManager.SaveAsync(); deferral.Complete();}
註冊完成後,開啟MainPage.xaml.cs 在SaveState 方法中添加如下代碼,使應用掛起時能將Name 欄位儲存起來。
protected override void SaveState(Dictionary<String, Object> pageState){ pageState["name"] = nameInput.Text;}恢複
掛起操作完成後,就要進行恢複操作,將暫存的資料恢複到應用中。再次開啟App.xaml.cs 在PreviousExecutionState 判斷為Terminated 時加入SuspensionManager.RestoreAsync 方法恢複以前的應用狀態。
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 active if (rootFrame == null) { // Create a Frame to act as the navigation context and navigate to the first rootFrame = new Frame(); SuspensionDemo.Common.SuspensionManager.RegisterFrame(rootFrame, "appFrame"); if (args.PreviousExecutionState == ApplicationExecutionState.Terminated) { //TODO: Load state from previously suspended application await SuspensionDemo.Common.SuspensionManager.RestoreAsync(); } // Place the frame in the current Window Window.Current.Content = rootFrame; } if (rootFrame.Content == null) { // When the navigation stack isn't restored navigate to the first page, // configuring the new page by passing required information as a navigation // parameter if (!rootFrame.Navigate(typeof(MainPage), args.Arguments)) { throw new Exception("Failed to create initial page"); } } // Ensure the current window is active Window.Current.Activate();}
最後,在MainPage.xaml.cs 的LoadState 方法中將pageState的Name 欄位內容恢複即可。我們再次F5運行應用;錄入姓名;掛起並終止應用,應用恢複後可以看到之前錄入的姓名仍然存在。
protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState){ if (pageState != null && pageState.ContainsKey("name")) { nameInput.Text = pageState["name"].ToString(); }}源碼下載
http://sdrv.ms/U1zyQd