Windows Phone三種共享數據的方式
***種方法:訪問公共屬性
在重寫protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)方法時,參數e包含了大量的數據,其中e.Content表示將要導航到的頁面,可以直接通過e.Content來訪問將要導航到的頁面的公共全局變量。如
- (e.Content as SecondPage).textBlock1.Text = "ddd";
 
第二種方法:使用App類
首先要知道,程序中所有頁面都可以訪問到從Application派生的App類,可以通過往App類添加屬性、字段等來作為各個頁面都可以訪問的共享數據。訪問Application類的靜態(tài)屬性Current可以返回當前應用程序的Application對象,Current 屬性返回對 Application 對象的引用,而非從 Application 派生的類的實例。如果您需要直接訪問后者,則需要將由 Current 返回的值強制轉換為從 Application 派生的類的類型,如 (Application.Current as App).Str = "eee"; 其中Str是額外添加到App類的:
- public partial class App : Application { public string Str { set; get; } }
 
第三種方法:使用PhoneApplicationService對象的State屬性
PhoneApplicationService對象的State屬性 是 IDictionary<string, object>類型的字典接口。
該項目有兩個頁面:MainPage和SecondPage,各有三個button和三個textblock。代碼如下:
- View Code
 - 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;
 - using Microsoft.Phone.Shell;
 - namespace WPApp_Navigation
 - {
 - //為類App添加一個公共屬性.程序中所有頁面都可以訪問到從Application派生的App類
 - public partial class App : Application
 - {
 - public string Str { set; get; }
 - }
 - public partial class MainPage : PhoneApplicationPage
 - {
 - // 構造函數
 - public MainPage()
 - {
 - InitializeComponent();
 - this.button1.Click += new RoutedEventHandler(button1_Click);
 - }
 - void button1_Click(object sender, RoutedEventArgs e)
 - {
 - //賦給Uri的字符串參數中間別留空格,多個參數中間用&連起來
 - this.NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.RelativeOrAbsolute));
 - }
 - //重寫基類的OnNavigateFrom方法,離開此頁面時觸發(fā)
 - protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
 - {
 - //參數 e 表示為導航方法提供的數據
 - base.OnNavigatedFrom(e);
 - //***種方法:訪問公共屬性
 - //e.Content 表示正在導航到的目標
 - if (e.Content is SecondPage)
 - {
 - (e.Content as SecondPage).textBlock1.Text = "ddd";
 - }
 - //第二種方法:通過App類中的公共屬性
 - //Current 屬性返回對 Application 對象的引用
 - (Application.Current as App).Str = "eee";
 - //第三種方法:使用PhoneApplicationService對象的State屬性
 - PhoneApplicationService.Current.State["s6"] = "fff";
 - }
 - protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
 - {
 - base.OnNavigatedTo(e);
 - this.textBlock2.Text = (Application.Current as App).Str;
 - if (PhoneApplicationService.Current.State.ContainsKey("s6"))
 - {
 - this.textBlock3.Text = (string)(PhoneApplicationService.Current.State["s6"]);
 - }
 - }
 - }
 - }
 
- View Code
 - 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;
 - using Microsoft.Phone.Shell;
 - namespace WPApp_Navigation
 - {
 - public partial class SecondPage : PhoneApplicationPage
 - {
 - public int a = 0;
 - int b = 1;
 - public SecondPage()
 - {
 - InitializeComponent();
 - this.button1.Click += new RoutedEventHandler(button1_Click);
 - }
 - void button1_Click(object sender, RoutedEventArgs e)
 - {
 - //返回上一個頁面
 - this.NavigationService.GoBack();
 - }
 - //當該頁面是活動頁面時觸發(fā)
 - protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
 - {
 - base.OnNavigatedTo(e);//調用基類的虛方法是應該的,但不是必須的
 - this.textBlock2.Text = (Application.Current as App).Str;
 - if (PhoneApplicationService.Current.State.ContainsKey("s6"))
 - {
 - this.textBlock3.Text = (string)PhoneApplicationService.Current.State["s6"];
 - }
 - }
 - //當該頁面不是活動頁面時觸發(fā)
 - protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
 - {
 - base.OnNavigatedFrom(e);
 - //離開此頁面前,該頁面也可以通過三種方式來傳遞數據給下一個頁面
 - if (e.Content is MainPage)
 - {
 - (e.Content as MainPage).textBlock1.Text = "123";
 - }
 - (Application.Current as App).Str = "456";
 - PhoneApplicationService.Current.State["s6"] = "789";
 - }
 - }
 - }
 















 
 
 




 
 
 
 