windows phone三种共享数据的方式

简介:

上篇文章简述了如果通过在uri后面添加参数来达到传递参数的目的,有时候不仅仅是需要传递数据,而是共享数据,又该如何?下面简述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类的静态属性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类型的字典接口

  该项目有两个页面:MainPage和SecondPage,各有三个button和三个textblock。代码如下:


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 }
 

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 }








本文转自 wws5201985 51CTO博客,原文链接:http://blog.51cto.com/wws5201985/811888,如需转载请自行联系原作者
目录
相关文章
|
Android开发 iOS开发 Windows
Windows Phone 寿终正寝了,这些经典机型你还记得吗?
不久前,随着最后一家WP手机厂商惠普宣布取消今后Windows Phone的研发计划,以及微软官方声明对WP8.1系统今后所有升级维护的终止,WP手机,作为曾经和安卓手机、苹果手机并驾齐驱的三大智能手机之一,正式寿终正寝。
1251 0
Windows Phone 寿终正寝了,这些经典机型你还记得吗?
|
XML 开发框架 前端开发
Windows Phone快速入门需掌握哪些能力
在此之前,先普及下Windows Phone的概念和开发工具的介绍。 Windows Phone是微软公司开发的手机操作系统,它将微软旗下的Xbox Live游戏、Xbox Music音乐与独特的视频体验集成至手机中。2012年6月21日,微软正式发布Windows Phone 8,采用和Windows 8相同的Windows NT内核,同时也针对市场的Windows Phone 7.5发布Windows Phone 7.8。
135 0
Windows Phone快速入门需掌握哪些能力
|
编解码 前端开发 JavaScript
Windows Phone 下开发 LBS 应用
基于位置的服务(Location Based Service,LBS),它是通过电信移动运营商的无线电通讯网络(如GSM网、CDMA网)或外部定位方式(如GPS)获取移动终端用户的位置信息(地理坐标,或大地坐标),在GIS(Geographic Information System,地理信息系统)平台的支持下,为用户提供相应服务的一种增值业务。
162 0
|
移动开发 Android开发 开发者
Windows Phone 8.1 新功能汇总 开发者预览版开放下载
在Build 2014大会上,微软正式发布了传闻已久的Windows Phone 8.1系统,所有的Windows Phone 8手机都可以升级,微软这次可谓是十分厚道。虽然并非迭代升级,但WP 8.1还是拥有很多重大更新,对于微软进一步完善移动平台拥有积极的意义。下面,就一起来了解一下WP 8.1的主要新特性。
230 0
Windows Phone 8.1 新功能汇总 开发者预览版开放下载
|
Windows 数据安全/隐私保护 C#