windows phone 學習之三種共用資料的方式

來源:互聯網
上載者:User

上篇文章簡述了如果通過在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 }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.