WP7 頁面之間參數傳遞方法

來源:互聯網
上載者:User

目前對WP7開發正在研究,對頁面之間參數傳遞進行了一個小總結,有不正確的地方,歡迎大家指正。。

WP7編程採用的技術是Silverlight,頁面之間參數傳遞的方式主要有

  1. 通過NavigationContext的QueryString方式;
  2. 通過程式的App類設定全域變數;
  3. 通過PhoneApplicationService類的State屬性;
  4. 通過NavigationEventArgs事件類別的Content屬性設定 

1.通過NavigationContext的QueryString函數。

首先通過NavigationService類進行設定導航至Page1頁面。

NavigationService.Navigate(new Uri("/Page1.xaml?id=1",UriKind.Relative));
在Page1頁面的PhoneApplicationPage_Loaded方法中可以通過NavigationContext的QueryString方法擷取傳遞的參數值,如下所示。
 
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e){int id =  int.Parse(NavigationContext.QueryString["id"]);}

2. 通過程式的App類設定全域變數;

由於App 類繼承自Application類,而通過Application的Current屬性可以擷取到與當前程式關聯的Application類執行個體,然後通過轉換就可以得到App類執行個體,因此,通過在App類中設定全域變數,在程式的其他任何頁面都可以訪問。 

public partial class App:Application{       public int ID { get; set;}}

假設從頁面Page1中需要把參數傳遞給Page2頁面中,可以先在Page1頁面中先設定;

App app = Application.Current as App;app.Id= 1; //設定傳遞參數;

在Page2頁面擷取設定的參數值;

App app = Application.Current as App;int id = app.ID; //擷取在Page1頁面傳遞參數;

3. 通過PhoneApplicationService類的State屬性;

由於PhoneApplicationService類的State是一個IDictionary類型,因此,可以儲存任何對象,不過這個對象必須是可序列化(serializable)的。

註:PhoneApplicationService類,需要訪問命名空間using Microsoft.Phone.Shell;

在程式中,可以不需要自己建立PhoneApplicationService的執行個體,通過PhoneApplicationService的靜態屬性Current就可以擷取到已有的PhoneApplicationService執行個體

在Page1頁面中設定參數;

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e){     phoneAppService.State["id"] = int.Parse(myTextBox.Text);//擷取Page1頁面的值,進行傳遞;     base.OnNavigatedFrom(e);} protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) {    object myTxt = null;    if (phoneAppService.State.ContainsKey("id"))    {         if (phoneAppService.State.TryGetValue("id", out myTxt))         {              myTextBox.Text = myTxt.ToString();         }    }    base.OnNavigatedTo(e); }

在Page2中擷取

protectedoverridevoidOnNavigatedTo(System.Windows.Navigation.NavigationEventArgse){if(PhoneApplicationService.Current.State.ContainsKey("id")){  myTextBlock.Text=PhoneApplicationService.Current.State["id"] as string;}base.OnNavigatedTo(e);}protectedoverridevoidOnNavigatedFrom(System.Windows.Navigation.NavigationEventArgse){PhoneApplicationService.Current.State["id"]=myTextBlock.Text;base.OnNavigatedFrom(e);}

4. 通過NavigationEventArgs事件類別的Content屬性設定 

在導航至其他頁面函數OnNavigatedFrom中,測試導航的目標頁面是否為自己真正要轉向傳遞參數的頁面,如果是,可以通過NavigationEventArgs事件類別的向目標頁面注入一些"傳遞內容"。

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e){    var targetPage = e.Content as Page2;    if (targetPage!=null)    {        targetPage.ID= 1; //設定參數值    }}

在頁面Page2中擷取參數值;

public int ID { get; set; }protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e){    if (param4 != null)    {        textBlock3.Text = ID.ToString(); //擷取參數值;
    }}  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.