欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > 内容正文

windows phone 学习之三种共享数据的方式

来源: 开发者 投稿于  被查看 34896 次 评论:108

windows phone 学习之三种共享数据的方式


上篇文章http://www.2cto.com/kf/201203/123913.html 简述了如果通过在uri后面添加参数来达到传递参数的目的,有时候不仅仅是需要传递数据,而是共享数据,又该如何?下面简述共享数据的三种方法。
第一种方法:访问公共属性
在重写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
 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
15 namespace WPApp_Navigation
16 {
17     //为类App添加一个公共属性.程序中所有页面都可以访问到从Application派生的App类
18     public partial class App : Application
19     {
20         public string Str { set; get; }
21     }
22
23     public partial class MainPage : PhoneApplicationPage
24     {       
25         // 构造函数
26         public MainPage()
27         {
28             InitializeComponent();
29             this.button1.Click += new RoutedEventHandler(button1_Click);                       
30         }
31
32         void button1_Click(object sender, RoutedEventArgs e)
33         {
34             //赋给Uri的字符串参数中间别留空格,多个参数中间用&连起来
35             this.NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.RelativeOrAbsolute));
36         }
37
38         //重写基类的OnNavigateFrom方法,离开此页面时触发
39         protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
40         {
41             //参数 e 表示为导航方法提供的数据
42             base.OnNavigatedFrom(e);
43
44             //第一种方法:访问公共属性
45 //e.Content 表示正在导航到的目标
46             if (e.Content is SecondPage)
47             {
48                 (e.Content as SecondPage).textBlock1.Text = "ddd";
49             }
50
51             //第二种方法:通过App类中的公共属性
52 //Current 属性返回对 Application 对象的引用
53             (Application.Current as App).Str = "eee";
54
55             //第三种方法:使用PhoneApplicationService对象的State属性
56             PhoneApplicationService.Current.State["s6"] = "fff";
57         }
58
59         protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
60         {
61             base.OnNavigatedTo(e);
62
63             this.textBlock2.Text = (Application.Current as App).Str;
64
65             if (PhoneApplicationService.Current.State.ContainsKey("s6"))
66             {
67                 this.textBlock3.Text = (string)(PhoneApplicationService.Current.State["s6"]);
68             }
69            
70         }
71     }
72 }
 
  View Code
 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
15 namespace WPApp_Navigation
16 {
17     public partial class SecondPage : PhoneApplicationPage
18     {
19         public int a = 0;
20         int b = 1;
21         public SecondPage()
22         {
23             InitializeComponent();
24             this.button1.Click += new RoutedEventHandler(button1_Click);      
25         }
26
27         void button1_Click(object sender, RoutedEventArgs e)
28         {
29             //返回上一个页面
30             this.NavigationService.GoBack();
31         }
32
33         //当该页面是活动页面时触发
34         protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
35         {
36            base.OnNavigatedTo(e);//调用基类的虚方法是应该的,但不是必须的
37
38            this.textBlock2.Text = (Application.Current as App).Str;
39
40            if (PhoneApplicationService.Current.State.ContainsKey("s6"))
41            {
42                this.textBlock3.Text = (string)PhoneApplicationService.Current.State["s6"];
43            }
44            
45         }
46
47         //当该页面不是活动页面时触发
48         protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
49         {
50             base.OnNavigatedFrom(e);
51
52             //离开此页面前,该页面也可以通过三种方式来传递数据给下一个页面
53             if (e.Content is MainPage)
54             {
55                 (e.Content as MainPage).textBlock1.Text = "123";
56             }
57
58             (Application.Current as App).Str = "456";
59
60             PhoneApplicationService.Current.State["s6"] = "789";
61         }     
62     }
63 }


 摘自 仙外仙

相关文章

    暂无相关文章
相关频道:

用户评论