Windows Phone 7 三种共享数据的方式

15 篇文章 0 订阅
14 篇文章 0 订阅
第一种方法:访问公共属性
在重写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。代码如下:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Shapes;
  12. using Microsoft.Phone.Controls;
  13. using Microsoft.Phone.Shell;

  14. namespace WPApp_Navigation
  15. {
  16.      //为类App添加一个公共属性.程序中所有页面都可以访问到从Application派生的App类
  17.      public partial class App : Application
  18.      {
  19.          public string Str { set; get; }
  20.      }

  21.      public partial class MainPage : PhoneApplicationPage
  22.      {        
  23.          // 构造函数
  24.          public MainPage()
  25.          {
  26.              InitializeComponent();
  27.              this.button1.Click += new RoutedEventHandler(button1_Click);                        
  28.          }

  29.          void button1_Click(object sender, RoutedEventArgs e)
  30.          {
  31.              //赋给Uri的字符串参数中间别留空格,多个参数中间用&连起来
  32.              this.NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.RelativeOrAbsolute));
  33.          }

  34.          //重写基类的OnNavigateFrom方法,离开此页面时触发
  35.          protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
  36.          {
  37.              //参数 e 表示为导航方法提供的数据
  38.              base.OnNavigatedFrom(e);

  39.              //第一种方法:访问公共属性
  40. //e.Content 表示正在导航到的目标
  41.              if (e.Content is SecondPage)
  42.              {
  43.                  (e.Content as SecondPage).textBlock1.Text = "ddd";
  44.              }

  45.              //第二种方法:通过App类中的公共属性
  46. //Current 属性返回对 Application 对象的引用
  47.              (Application.Current as App).Str = "eee";

  48.              //第三种方法:使用PhoneApplicationService对象的State属性
  49.              PhoneApplicationService.Current.State["s6"] = "fff";
  50.          }

  51.          protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
  52.          {
  53.              base.OnNavigatedTo(e);

  54.              this.textBlock2.Text = (Application.Current as App).Str;

  55.              if (PhoneApplicationService.Current.State.ContainsKey("s6"))
  56.              {
  57.                  this.textBlock3.Text = (string)(PhoneApplicationService.Current.State["s6"]);
  58.              }
  59.              
  60.          }
  61.      }
  62. }

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Shapes;
  12. using Microsoft.Phone.Controls;
  13. using Microsoft.Phone.Shell;

  14. namespace WPApp_Navigation
  15. {
  16.      public partial class SecondPage : PhoneApplicationPage
  17.      {
  18.          public int a = 0;
  19.          int b = 1;
  20.          public SecondPage()
  21.          {
  22.              InitializeComponent();
  23.              this.button1.Click += new RoutedEventHandler(button1_Click);       
  24.          }

  25.          void button1_Click(object sender, RoutedEventArgs e)
  26.          {
  27.              //返回上一个页面
  28.              this.NavigationService.GoBack();
  29.          }

  30.          //当该页面是活动页面时触发
  31.          protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
  32.          {
  33.             base.OnNavigatedTo(e);//调用基类的虚方法是应该的,但不是必须的

  34.             this.textBlock2.Text = (Application.Current as App).Str;

  35.             if (PhoneApplicationService.Current.State.ContainsKey("s6"))
  36.             {
  37.                 this.textBlock3.Text = (string)PhoneApplicationService.Current.State["s6"];
  38.             }
  39.              
  40.          }

  41.          //当该页面不是活动页面时触发
  42.          protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
  43.          {
  44.              base.OnNavigatedFrom(e);

  45.              //离开此页面前,该页面也可以通过三种方式来传递数据给下一个页面
  46.              if (e.Content is MainPage)
  47.              {
  48.                  (e.Content as MainPage).textBlock1.Text = "123";
  49.              }

  50.              (Application.Current as App).Str = "456";

  51.              PhoneApplicationService.Current.State["s6"] = "789";
  52.          }      
  53.      }
  54. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值