#眉標=特別報導 #副標=新一代智慧型手機平台Windows Phone 7開發專欄(2) #大標=保留執行中應用程式的資料 #作者=文/圖 董大偉 ---------------程式碼-------------------------- NavigationService.Navigate( new Uri("/Page1.xaml?para1=dat1¶2=dat2", UriKind.Relative)); ---------------End-------------------------- ---------------程式碼-------------------------- void Page1_Loaded(object sender, RoutedEventArgs e) { string dat = NavigationContext.QueryString["para1"]; this.textBox1.Text = dat; } ---------------End-------------------------- ---------------程式碼-------------------------- App.xaml.cs: public partial class App : Application { /// /// Provides easy access to the root frame of the Phone Application. /// /// The root frame of the Phone Application. public PhoneApplicationFrame RootFrame { get; private set; } public string globalData1 = ""; ---------------End-------------------------- ---------------程式碼-------------------------- Page1.xaml.cs: private void button1_Click(object sender, RoutedEventArgs e) { (App.Current as App).globalData1 = this.textBox2.Text; } ---------------End-------------------------- ---------------程式碼-------------------------- MainPage1.xaml.cs: private void button3_Click(object sender, RoutedEventArgs e) { this.textBox2.Text = (App.Current as App).globalData1; } ---------------End-------------------------- -----------------程式碼------------- App.Xaml.cs: private void Application_Deactivated(object sender, DeactivatedEventArgs e) { PhoneApplicationService.Current.State["globalData1"] = globalData1; } ----------------End------------- 同時,在應用程式從凍結模式返回時,將資料還原: -----------------程式碼------------- App.Xaml.cs: private void Application_Activated(object sender, ActivatedEventArgs e) { object obj; PhoneApplicationService.Current.State.TryGetValue("globalData1", out obj); if (obj != null) globalData1 = obj.ToString(); } -----------------End-------------    --------------------程式碼---------------------- Page1.xaml.cs: protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) { this.State["textBox2"] = textBox2.Text; base.OnNavigatedFrom(e); } protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { object obj; this.State.TryGetValue("textBox2", out obj); if (obj != null) textBox2.Text = obj.ToString(); base.OnNavigatedTo(e); } -------------------End---------------------- ------------------程式碼------------------ MainPage.xaml.cs: public MainPage() { InitializeComponent(); BackKeyPress += (ss,arg) => { if (MessageBox.Show("確定要離開???", "確認", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel) { arg.Cancel = true; } }; } -------------------End------------ ------------------程式碼------------------ Page1 的Loaded事件: public partial class Page1 : PhoneApplicationPage { public Page1() { InitializeComponent(); Loaded += new RoutedEventHandler(Page1_Loaded); } void Page1_Loaded(object sender, RoutedEventArgs e) { this.Storyboard1.Begin(); …略… } ------------------End------------------ ----------------程式碼------------------------- Page1.xaml.cs: public Page1() { InitializeComponent(); (…略…) OrientationChanged += (ss, arg) => { //可在這裡撰寫程式碼進行處理 }; } ---------------End------------