Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ch07 生命週期(Life Cycle).

Similar presentations


Presentation on theme: "Ch07 生命週期(Life Cycle)."— Presentation transcript:

1 Ch07 生命週期(Life Cycle)

2 為何要了解及管理App的生命週期 手機的資源(Power,記憶體,)是有限的
手機作業系統一般在同一時間只能一個App 在前景執行 (foreground App) 其他的App則被 suspended 或 terminated 手機作業系統有不同的機制讓一個App轉換到前景執行 手機作業系統有不同的機制讓一個App在背景執行

3 Window Phone App的生命週期 App還未啟動(launch)是在NotRunning的狀 態。
當使用者執行App後,它進入Running的狀 態。 當App進入啟動另一App,或是在Low Power的狀態它進入Supended 狀態。(App 不可見,但被存在記憶體中) 當Windows Phone轉換回(Resuming)此App, 此App回到Running的狀態。(此時的狀態 為App Suspended之前的狀態,資料都還 在) 當使用者關閉一個App或一個App在 suspended狀態,且此時作業系統因記憶 體不足自動將此App終止,會進入Not Running的狀態。 NotRunning state ( it has never been launched it was running but then crashed it was suspended but then couldn't be kept in memory and was terminated by the system. Activated (restore previously saved data ) (OnActivated event) Windows displays a splash screen for the app to register event handlers and set up any custom UI it needs for loading the initial page. If an app needs to request data from the network or needs to retrieve large amounts of data from disk, these activities should be completed outside of activation.  Suspended (save its state and release its exclusive resources and file handles immediately when handling the suspending event,) the user switches away from it when the device enters a low power state. When the user moves an app to the background, Windows waits a few seconds to see whether the user immediately switches back to the app. If the user does not switch back within this time window, Windows suspends the app. Windows attempts to keep as many suspended apps in memory as possible. However, if there aren't enough resources to keep your app in memory, Windows can terminate your app. Individual apps don't receive notification that they are being terminated, so the only opportunity you have to save your app's data is during suspension. Resuming event HTML apps usually don't need to handle resuming  If a suspended app is activated to participate in an app contract or extension, it receives the Resuming event first, then the Activated event. When an app is suspended, it does not receive network events that it registered to receive. These events are not queued, they are simply missed. Therefore, your app should test the network status when it is resumed. App close Generally, users don't need to close apps, they can let Windows manage them.  After an app has been closed by the user, it's suspended and terminated, and then enters the NotRunning state.

4 App Class 大部分生命週期相關的方法都是在App Class (App.xaml.cs) App的進入點 管理App的生命週期
OnLaunched():當App在初始啟動時執行。 OnSuspending():進入到Suspended狀態前執行,通常在此 將App的狀態存起來。 管理可在整個App被使用的資源,如 <Application.Resources>、Frame物件。

5 設定App啟動時第一個執行的Page 新增一WelcomePage.xaml
基本頁面會自動增加幾個Class,這些Class提供Navigation 和生命週期管理等功能

6 練習CH07-01 請完成上一張投影片的程式碼

7 在suspended前儲存Frame的瀏覽資料並在下次啟動時回復(1)
1. 在App.xaml.cs的OnLaunched方法註冊需要suspending 管理的 Frame You set the CacheSize property to specify how many pages can be retained in a cache. When a page is cached, an instance of the page is reused for each navigation request rather than re-creating the page for each request. XAML Metro app: Complex objects as parameter when navigating between pages causes crash The types stored by individual pages must be able to be serialized by the DataContractSerializer in C# and VB. To do this any custom type must be registered before it can be saved or restored. SuspensionManager provides the KnownTypes collection which passes the types in the collection to the DataContractSerializer. As the SuspensionManager is called to restore state in the OnLaunched override of the code behind for App.xaml, a good place to register types is in the app constructor. The parameters passed in using navigation must be able to be serialized by the platform. When we are saving and restoring the navigation stack, we call Frame.GetNavigationState() and Frame.SetNavigationState(). Both of these calls make use of an internal serialization format and all types passed as the parameter in Frame.Navigate() must be able to be serialized by the platform.

8 在suspended前儲存Frame的瀏覽資料並在下次啟動時回復(2)
2. 在App.xaml.cs的OnSuspending方法儲存Frame的狀態 You set the CacheSize property to specify how many pages can be retained in a cache. When a page is cached, an instance of the page is reused for each navigation request rather than re-creating the page for each request. XAML Metro app: Complex objects as parameter when navigating between pages causes crash The types stored by individual pages must be able to be serialized by the DataContractSerializer in C# and VB. To do this any custom type must be registered before it can be saved or restored. SuspensionManager provides the KnownTypes collection which passes the types in the collection to the DataContractSerializer. As the SuspensionManager is called to restore state in the OnLaunched override of the code behind for App.xaml, a good place to register types is in the app constructor. The parameters passed in using navigation must be able to be serialized by the platform. When we are saving and restoring the navigation stack, we call Frame.GetNavigationState() and Frame.SetNavigationState(). Both of these calls make use of an internal serialization format and all types passed as the parameter in Frame.Navigate() must be able to be serialized by the platform.

9 在suspended前儲存Frame的瀏覽資料並在下次啟動時回復(3)
3. 在App.xaml.cs的OnLaunched方法回復Frame的狀態 You set the CacheSize property to specify how many pages can be retained in a cache. When a page is cached, an instance of the page is reused for each navigation request rather than re-creating the page for each request. XAML Metro app: Complex objects as parameter when navigating between pages causes crash The types stored by individual pages must be able to be serialized by the DataContractSerializer in C# and VB. To do this any custom type must be registered before it can be saved or restored. SuspensionManager provides the KnownTypes collection which passes the types in the collection to the DataContractSerializer. As the SuspensionManager is called to restore state in the OnLaunched override of the code behind for App.xaml, a good place to register types is in the app constructor. The parameters passed in using navigation must be able to be serialized by the platform. When we are saving and restoring the navigation stack, we call Frame.GetNavigationState() and Frame.SetNavigationState(). Both of these calls make use of an internal serialization format and all types passed as the parameter in Frame.Navigate() must be able to be serialized by the platform.

10 在suspended前儲存Frame的瀏覽資料並在下次啟動時回復(4)
SuspensionManager只能儲存 基本型態的資料如int, string,所以在 傳遞多個參數時要將其以字串傳遞 使用”週期事件”下拉選單轉換App狀態 Frame.Navigate(typeof(ShowName),user); Frame.Navigate(typeof(ShowName), this.tbUserName.Text + "," + this.pbPassword.Password + "," + "Assets/ko.jpg"); User user = (User)e.Parameter; string[] user = e.Parameter.ToString().Split(','); You set the CacheSize property to specify how many pages can be retained in a cache. When a page is cached, an instance of the page is reused for each navigation request rather than re-creating the page for each request. XAML Metro app: Complex objects as parameter when navigating between pages causes crash The types stored by individual pages must be able to be serialized by the DataContractSerializer in C# and VB. To do this any custom type must be registered before it can be saved or restored. SuspensionManager provides the KnownTypes collection which passes the types in the collection to the DataContractSerializer. As the SuspensionManager is called to restore state in the OnLaunched override of the code behind for App.xaml, a good place to register types is in the app constructor. The parameters passed in using navigation must be able to be serialized by the platform. When we are saving and restoring the navigation stack, we call Frame.GetNavigationState() and Frame.SetNavigationState(). Both of these calls make use of an internal serialization format and all types passed as the parameter in Frame.Navigate() must be able to be serialized by the platform.

11 練習CH07-02 請完成上一張投影片的程式碼

12 在suspended前儲存資料並在下次啟動時回復(1)
註冊事件

13 在suspended前儲存資料並在下次啟動時回復(2)
在OnNavigatedTo方法增加this.navigationHelper.OnNavigatedTo(e); 在OnNavigatedFrom方法增加this.navigationHelper.OnNavigatedFrom(e);

14 在suspended前儲存資料並在下次啟動時回復(3)
增加NavigationHelper_SaveState方法 SaveStateEventArgs有一個property PageState,PageState是一個dictionary用以存 資料

15 在suspended前儲存資料並在下次啟動時回復(4)
增加NavigationHelper_LoadState方法

16 練習CH07-03 請完成上一張投影片的程式碼


Download ppt "Ch07 生命週期(Life Cycle)."

Similar presentations


Ads by Google