***种方法:访问公共属性
在重写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。代码如下:
- 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方法,离开此页面时触发
- 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();
- }
- //当该页面是活动页面时触发
- 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"];
- }
- }
- //当该页面不是活动页面时触发
- 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";
- }
- }
- }