成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Windows Phone三種共享數(shù)據(jù)的方式

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

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

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

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

第二種方法:使用App類

首先要知道,程序中所有頁面都可以訪問到從Application派生的App類,可以通過往App類添加屬性、字段等來作為各個頁面都可以訪問的共享數(shù)據(jù)。訪問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.          // 構造函數(shù) 
  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的字符串參數(shù)中間別留空格,多個參數(shù)中間用&連起來 
  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.              //參數(shù) e 表示為導航方法提供的數(shù)據(jù) 
  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.              //離開此頁面前,該頁面也可以通過三種方式來傳遞數(shù)據(jù)給下一個頁面 
  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

數(shù)據(jù)中心模塊化

2020-11-01 17:10:46

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

2021-11-05 21:33:28

Redis數(shù)據(jù)高并發(fā)

2019-11-20 18:52:24

物聯(lián)網(wǎng)智能照明智能恒溫器

2014-12-31 17:42:47

LBSAndroid地圖

2021-06-24 08:52:19

單點登錄代碼前端

2010-03-12 17:52:35

Python輸入方式

2018-04-08 09:31:57

大數(shù)據(jù)

2024-07-08 09:03:31

2011-06-03 11:53:06

Spring接口

2015-01-05 09:56:20

可穿戴設備

2023-10-18 11:12:01

增強現(xiàn)實VR

2009-07-20 15:08:41

Spring實例化Be

2022-10-18 10:41:44

Flowable服務任務

2022-08-19 11:19:49

單元測試Python

2010-08-24 09:43:33

2023-08-22 07:05:34

PowerShellWindows
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久99精品国产 | 日一区二区 | 日韩欧美国产不卡 | 亚洲国产高清免费 | 男人的天堂久久 | 美女午夜影院 | 久久99精品久久久久久国产越南 | 精品久久电影 | 国产精品久久久久久久久久久免费看 | 免费在线成人 | 久久久精彩视频 | 91视频日本 | 一级电影免费看 | 91网视频 | 欧美电影一区 | 亚洲一区二区三区四区五区午夜 | 中文字幕国产 | 成人免费视频网站在线看 | 亚洲精品一区二区三区在线观看 | 国产资源一区二区三区 | 欧美视频一区二区三区 | 亚洲a视频 | 红色av社区 | 涩爱av一区二区三区 | 亚洲精品在线视频 | 久久久精品视频一区二区三区 | 亚洲国产专区 | 99久久免费精品视频 | 特级毛片爽www免费版 | 国产福利小视频 | 日韩欧美在线免费观看视频 | 久久久av| 91国内精精品久久久久久婷婷 | 国产视频久久久 | 久久亚洲一区二区三区四区 | 亚洲人成人一区二区在线观看 | 国产欧美综合在线 | 国产中文字幕在线观看 | 日韩视频在线播放 | 欧美极品少妇xxxxⅹ免费视频 | 亚洲午夜精品久久久久久app |