Windows Phone 7应用程序生命周期

Windows Phone 7的生命周期分为4个状态,即:

Launching:程序启动

Activated:程序被激活

Deactivated:程序被冻结

Closing:程序被关闭

一、墓碑机制:

当应用程序被导航到别的页面时,当前的应用程序将会被冻结,系统会保存一些应用程序在被导航之前的一些信息,当我们在此回到该程序的时候,系统会读取我们保存的信息,使应用程序恢复到我们之前的状态。因此程序员必须要使用代码来托管应用程序的生命周期。

墓碑机制的生命周期:

2、生命周期的处理事件

Application_Launching

程序启动时调用

程序重新激活时不被调用

Application_Activated

程序重新激活时调用

程序启动时不被调用

Application_Deactivated

程序失去激活时调用

程序关闭时不调用

Application_Closing

程序关闭时调用

程序失去激活时不被调用

同时在

在解决方案的App.xaml.cs中会自动定义以上几种事件的处理函数:

// Code to execute when the application is launching (eg, from Start)          // This code will not execute when the application is reactivated    private void Application_Launching(object sender, LaunchingEventArgs e)          {  //添加自定义代码,下同  Debug.WriteLine("Application_Launching DateTime:" + DateTime.Now.ToLongTimeString());          }          // Code to execute when the application is activated (brought to foreground)          // This code will not execute when the application is first launched          private void Application_Activated(object sender, ActivatedEventArgs e)          {  Debug.WriteLine("Application_Activated DateTime:" + DateTime.Now.ToLongTimeString());          }          // Code to execute when the application is deactivated (sent to background)          // This code will not execute when the application is closing          private void Application_Deactivated(object sender, DeactivatedEventArgs e)          {  Debug.WriteLine("Application_Deactivated DateTime:" + DateTime.Now.ToLongTimeString());          }          // Code to execute when the application is closing (eg, user hit Back)          // This code will not execute when the application is deactivated          private void Application_Closing(object sender, ClosingEventArgs e)          {  Debug.WriteLine("Application_Closing DateTime:" + DateTime.Now.ToLongTimeString());          }

需要注意的是:应用程序在被冻结之后,不一定在之后会被激活,当应用程序运行过多时,WindowsPhone会自行判断,并关闭一些应用程序。Windows Phone是多任务系统,但是没有开发第三方应用程序的多任务,WindowsPhone只允许一个第三方软件在前台运行。

我们现在做一个小例子来演示墓碑机制,在此还学要说明的两个问题是关于Windows Phone 7的(应用程序)永久数据和临时数据。

3、Windows Phone 7的(应用程序)永久数据和临时数据。

Windows Phone 7的临时数据是指数据保存在一个内存中,当程序被冻结即Deactivated,这个时候程序会保存临时数据,通过PhoneApplicationService.Current.State来保存临时数据。当程序再次被激活的时候则可以读取到之前保存的数据,那么该数据即为临时数据,关于临时数据保存的地方还不是很确定,一般认为保存在内存中。

现在我们拖放一个TextBlock空间和一个TextBox空间,将TextBlock的Text属性设置为永久数据,将TextBox的Name属性设置为PersistDataTextBox.

然后在App中定义两个类级别的变量:

public static string PesistedData { get; set; }  private const string PersistedSetting = "MySettingData";

之后我们在程序的启动事件函数(Application_Launching)和关闭事件函数(Application_Closing)中添加如下代码:

 private void Application_Launching(object sender, LaunchingEventArgs e)          {  //程序启动时获得应用程序的存储空间  using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())              {   //如果应用程序的隔离存储空间包含PersistedSetting这个Key,即数据                  if(IsolatedStorageSettings.ApplicationSettings.Contains(PersistedSetting))         //将PersistedSetting的这个Key值读出来,赋给之前声明的变量。        PesistedData = IsolatedStorageSettings.ApplicationSettings[PersistedSetting] as string;              }  //输出调试信息  Debug.WriteLine("Application_Launching DateTime:" + DateTime.Now.ToLongTimeString());          }  private void Application_Closing(object sender, ClosingEventArgs e)          {  using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())              {  //当程序关闭时将PersistedSetting保存到应用程序的隔离存储空间之中  IsolatedStorageSettings.ApplicationSettings[PersistedSetting] = PesistedData;              }  Debug.WriteLine("Application_Closing DateTime:" + DateTime.Now.ToLongTimeString());          }

当我们添加以上代码时,实现的功能是当应用程序启动时,从应用程序的隔离存储空间中读取相应的数据,Close时保存相应的数据,但是在这里我们发现我们要保存的数据变量(PersistedSetting)没有和其他的数据挂钩(例如我们之前的拖放的TextBox控件的值)。所以我们需要将保存数据的变量(PersistedSetting)和要保存的数据值(PersistedTextBox.Text)联系起来。这里可以通过重写页面的OnNavigatedFrom和OnNavigatedTo方法来实现。下面就来重写TextBox控件所在页面的OnNavigatedFrom和OnNavigatedTo方法。代码如下:

  //从当前页面退出时触发该函数  protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)          {           //当页面离开时,即退出当前应用程序页面,那么就保存TextBox控件的Text值              App.PesistedData = PersistedTextBox.Text;              base.OnNavigatedFrom(e);          }          //进入到该页面时触发该函数  protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)          {              if (App.PesistedData != null)              {           //当程序回到之前的页面时则读取之前保存的数据                  PersistedTextBox.Text = App.PesistedData;              }              base.OnNavigatedTo(e);          }

通过以上的过成便实现了永久数据的读取和保存。临时数据的保存和永久数据的保存类似,只是我们不是在程序的启动和关闭事件函数中去读取数据和保存数据,而是在应用程序的冻结和激活事件函数中添加相应的代码,具体操作和保存永久数据一样,不同的是我们通过PhoneApplicationService.Current.State 来保存、读取数据。

关键代码:

//程序冻结时,保存数据  PhoneApplicationService.Current.State[TransientDataService] = TransientData;    //程序激活时读取数据  if (PhoneApplicationService.Current.State.ContainsKey(TransientDataService))              {  TransientData = PhoneApplicationService.Current.State[TransientDataService] as string;                  PhoneApplicationService.Current.State.Remove(TransientDataService);              }

在添加以上的程序之后,同样页面的OnNavigatedFrom和OnNavigatedTo方法中将保存数据的变量和数据值挂钩即可。

4、Windows phone 7生命周期总结:

5、页面状态的恢复

当应用程序激活时,应该恢复到之前的页面显示的状态。例如:

TextBox的光标

ScrollViewer的位置

DeepZoom的位置

我们可以通过PhoneApplicationPage.State(); 来恢复到之前的页面状态

示例代码:

private const string PageState = "Pagestate1";  //从当前页面退出时触发该函数          protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)          {              State[PageState] = PageState.Text;              base.OnNavigatedFrom(e);          }    //进入到该页面时触发该函数  protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)          {              if (State.ContainsKey(PageState))              {                  PageState.Text = State[PageState] as string;              }              base.OnNavigatedTo(e);          }

过程和之前保存永久数据或者临时数据相似。

 

以上内容参考了Jake Lin的视频,强烈推荐入手WP的新手看一看。

(版权所有,转载请标明出处)