偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Windows Phone三種共享數據的方式

移動開發(fā)
本文為大家介紹了Windows Phone開發(fā)中三種共享數據的方式,包括訪問公共屬性、使用App類、使用PhoneApplicationService對象的State屬性,希望對大家有所幫助。

***種方法:訪問公共屬性

在重寫protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)方法時,參數e包含了大量的數據,其中e.Content表示將要導航到的頁面,可以直接通過e.Content來訪問將要導航到的頁面的公共全局變量。如

  1. (e.Content as SecondPage).textBlock1.Text = "ddd"

第二種方法:使用App類

首先要知道,程序中所有頁面都可以訪問到從Application派生的App類,可以通過往App類添加屬性、字段等來作為各個頁面都可以訪問的共享數據。訪問Application類的靜態(tài)屬性Current可以返回當前應用程序的Application對象,Current 屬性返回對 Application 對象的引用,而非從 Application 派生的類的實例。如果您需要直接訪問后者,則需要將由 Current 返回的值強制轉換為從 Application 派生的類的類型,如 (Application.Current as App).Str = "eee"; 其中Str是額外添加到App類的:

  1. public partial class App : Application     {     public string Str { setget; }   } 

第三種方法:使用PhoneApplicationService對象的State屬性

PhoneApplicationService對象的State屬性 是 IDictionary<string, object>類型的字典接口。

該項目有兩個頁面:MainPage和SecondPage,各有三個button和三個textblock。代碼如下:

  1. View Code 
  2. using System; 
  3. using System.Collections.Generic; 
  4. using System.Linq; 
  5. using System.Net; 
  6. using System.Windows; 
  7. using System.Windows.Controls; 
  8. using System.Windows.Documents; 
  9. using System.Windows.Input; 
  10. using System.Windows.Media; 
  11. using System.Windows.Media.Animation; 
  12. using System.Windows.Shapes; 
  13. using Microsoft.Phone.Controls; 
  14. using Microsoft.Phone.Shell; 
  15. namespace WPApp_Navigation 
  16.      //為類App添加一個公共屬性.程序中所有頁面都可以訪問到從Application派生的App類 
  17.      public partial class App : Application 
  18.      { 
  19.          public string Str { setget; } 
  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方法,離開此頁面時觸發(fā) 
  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. }
  1. View Code 
  2. using System; 
  3. using System.Collections.Generic; 
  4. using System.Linq; 
  5. using System.Net; 
  6. using System.Windows; 
  7. using System.Windows.Controls; 
  8. using System.Windows.Documents; 
  9. using System.Windows.Input; 
  10. using System.Windows.Media; 
  11. using System.Windows.Media.Animation; 
  12. using System.Windows.Shapes; 
  13. using Microsoft.Phone.Controls; 
  14. using Microsoft.Phone.Shell; 
  15. namespace WPApp_Navigation 
  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.          //當該頁面是活動頁面時觸發(fā) 
  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.          //當該頁面不是活動頁面時觸發(fā) 
  41.          protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) 
  42.          { 
  43.              base.OnNavigatedFrom(e); 
  44.              //離開此頁面前,該頁面也可以通過三種方式來傳遞數據給下一個頁面 
  45.              if (e.Content is MainPage) 
  46.              { 
  47.                  (e.Content as MainPage).textBlock1.Text = "123"
  48.              } 
  49.              (Application.Current as App).Str = "456"
  50.              PhoneApplicationService.Current.State["s6"] = "789"
  51.          }       
  52.      } 

 

責任編輯:閆佳明 來源: wpcome
相關推薦

2017-07-14 15:07:23

2012-07-17 09:16:16

SpringSSH

2012-05-28 09:54:09

APP 性能

2015-01-16 17:41:45

數據中心模塊化

2020-11-01 17:10:46

異步事件開發(fā)前端

2019-11-20 18:52:24

物聯網智能照明智能恒溫器

2021-11-05 21:33:28

Redis數據高并發(fā)

2021-06-24 08:52:19

單點登錄代碼前端

2014-12-31 17:42:47

LBSAndroid地圖

2010-03-12 17:52:35

Python輸入方式

2018-04-08 09:31:57

大數據

2023-10-18 11:12:01

增強現實VR

2015-01-05 09:56:20

可穿戴設備

2009-07-20 15:08:41

Spring實例化Be

2011-06-03 11:53:06

Spring接口

2022-08-19 11:19:49

單元測試Python

2022-10-18 10:41:44

Flowable服務任務

2024-07-08 09:03:31

2018-04-02 14:29:18

Java多線程方式

2021-11-26 11:07:14

cowsay命令Linux
點贊
收藏

51CTO技術棧公眾號