第一种方法:访问公共属性
在重写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类的静态属性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。代码如下:
在重写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类的静态属性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。代码如下: