日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

windows phone 学习之页面导航和数据传递

發(fā)布時(shí)間:2024/10/12 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 windows phone 学习之页面导航和数据传递 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

創(chuàng)建一個(gè)windows phone 應(yīng)用程序,在xaml文件里添加三個(gè)按鈕和三個(gè)textblock,添加一個(gè)windows phone 頁(yè)面(命名為SecondPage),同樣也是添加三個(gè)按鈕和三個(gè)textblock。要實(shí)現(xiàn)的目標(biāo)是從MainPage 頁(yè)面導(dǎo)航到 SecondPage 頁(yè)面,并且能把一些參數(shù)傳遞過(guò)去,之后還能從 SecondPage 返回到 MainPage。

MainPage.xaml.cs 代碼如下:

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
14 namespace WPApp_Navigation
15 {
16 public partial class MainPage : PhoneApplicationPage
17 {
18 // 構(gòu)造函數(shù)
19 public MainPage()
20 {
21 InitializeComponent();
22
23 this.button1.Click += new RoutedEventHandler(button1_Click);
24 }
25
26 void button1_Click(object sender, RoutedEventArgs e)
27 {
28 // string path = "/SecondPage.xaml";
29 // string target = path + string.Format("?s1={0}","zou");
30
31 //賦給Uri的字符串參數(shù)中間別留空格,多個(gè)參數(shù)中間用&連起來(lái)
32 this.NavigationService.Navigate(new Uri("/SecondPage.xaml?s1=aaa&s2=bbb&s3=ccc", UriKind.RelativeOrAbsolute));
33 }
34 }
35 }

?

代碼this.NavigationService.Navigate(new Uri("/SecondPage.xaml?s1=aaa&s2=bbb&s3=ccc", UriKind.RelativeOrAbsolute));是核心

新建一個(gè)Uri對(duì)象,Uri里的第一個(gè)參數(shù)里面的/SecondPage.xaml 表示將要導(dǎo)航到的頁(yè)面的路徑,問(wèn)號(hào)后面的就是要傳遞過(guò)去的參數(shù),s1、s2、s3都是參數(shù)名 ,等號(hào)后面的是它們的值,參數(shù)之間用 & 連起來(lái);Uri的第二個(gè)參數(shù) UriKind.RelativeOrAbsolute 表示前面的路徑是相對(duì)的還是絕對(duì)的,RelativeOrAbsolute 表示 可能是相對(duì)的也可能是絕對(duì)的。

?

SecondPage.xaml.cs 代碼如下:

?

1 using System.Windows.Media.Animation;
2 using System.Windows.Shapes;
3 using Microsoft.Phone.Controls;
4
5 namespace WPApp_Navigation
6 {
7 public partial class SecondPage : PhoneApplicationPage
8 {
9 public SecondPage()
10 {
11 InitializeComponent();
12 this.button1.Click += new RoutedEventHandler(button1_Click);
13
14 }
15
16 void button1_Click(object sender, RoutedEventArgs e)
17 {
18 //返回上一個(gè)頁(yè)面
19 this.NavigationService.GoBack();
20 }
21
22 //當(dāng)該頁(yè)面是活動(dòng)頁(yè)面時(shí)觸發(fā)
23 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
24 {
25 base.OnNavigatedTo(e);//調(diào)用基類(lèi)的虛方法是應(yīng)該的,但不是必須的
26
27 if (NavigationContext.QueryString.ContainsKey("s1"))
28 {
29 this.textBlock1.Text = NavigationContext.QueryString["s1"];
30 this.textBlock2.Text = NavigationContext.QueryString["s2"];
31 this.textBlock3.Text = NavigationContext.QueryString["s3"];
32 }
33 }
34
35 //當(dāng)該頁(yè)面不是活動(dòng)頁(yè)面時(shí)觸發(fā)
36 protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
37 {
38 base.OnNavigatedFrom(e);
39 MessageBox.Show("離開(kāi)SecondPage");
40 }
41
42
43 //public void Get()
44 //{
45 ////不能這樣在自己寫(xiě)的方法里調(diào)用NavigationContext.QueryString
46 // IDictionary<string, string> text = NavigationContext.QueryString;
47 //}
48
49 }
50 }

在這里,重寫(xiě)基類(lèi)的OnNavigatedTo方法: protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)

OnNavigatedTo方法會(huì)在當(dāng)前頁(yè)面成為活動(dòng)時(shí)被觸發(fā),在這里,可以使用 NavigationContext.QueryString 來(lái)獲取從MainPage傳遞過(guò)來(lái)的參數(shù)。NavigationContext.QueryString 的類(lèi)型是 IDictionary<string, string> ,就是 鍵/值 對(duì)。

當(dāng)我們想回到MainPage 頁(yè)面時(shí),可以通過(guò) this.NavigationService.GoBack(); 實(shí)現(xiàn)

在離開(kāi)SecondPage 頁(yè)面時(shí),如果我們想在離開(kāi)前再做最后一點(diǎn)事情,可以放在 OnNavigatedFrom 函數(shù)里,也是通過(guò)重寫(xiě)基類(lèi)的虛方法實(shí)現(xiàn),當(dāng)該頁(yè)面從活動(dòng)變成不是活動(dòng)頁(yè)面時(shí)觸發(fā) protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/zouzf/archive/2012/03/19/2406224.html

總結(jié)

以上是生活随笔為你收集整理的windows phone 学习之页面导航和数据传递的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。