www.pudn.com > ApplicationLifecycle.zip > MainPage.xaml.cs, change:2011-07-10,size:1792b


using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using Microsoft.Phone.Controls; 
 
namespace ApplicationLifecycle 
{ 
    public partial class MainPage : PhoneApplicationPage 
    { 
        private const string PageState = "PageState1";//用于状态保存 
        // Constructor 
        public MainPage() 
        { 
            InitializeComponent(); 
        } 
 
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)//OnNavigatedTo 
        { 
            if (App.PersistedData != null)//需要判断是否为空,防止发生异常。 
            { 
                PersistedDataTextBox.Text = App.PersistedData; 
            } 
            if (App.TransientData != null)//假如,不为空就将它读到TextBox中去 
            { 
                TransientDataTextBox.Text = App.TransientData; 
            } 
 
            if (State.ContainsKey(PageState))//判断是否存在 
            { 
                PageDataTextBox.Text = State[PageState] as string;//因为保存的是一个Object,所以需要string一下, PageDataTextBox是TextBox的名字 
            } 
            base.OnNavigatedTo(e); 
        } 
 
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) 
        { 
            App.PersistedData = PersistedDataTextBox.Text;//写入 
            App.TransientData = TransientDataTextBox.Text;//写入 
            State[PageState] = PageDataTextBox.Text; 
            base.OnNavigatedFrom(e); 
        } 
    } 
}