日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

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

發布時間:2024/10/12 windows 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 windows phone 学习之页面导航和数据传递 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

創建一個windows phone 應用程序,在xaml文件里添加三個按鈕和三個textblock,添加一個windows phone 頁面(命名為SecondPage),同樣也是添加三個按鈕和三個textblock。要實現的目標是從MainPage 頁面導航到 SecondPage 頁面,并且能把一些參數傳遞過去,之后還能從 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 // 構造函數
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的字符串參數中間別留空格,多個參數中間用&連起來
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));是核心

新建一個Uri對象,Uri里的第一個參數里面的/SecondPage.xaml 表示將要導航到的頁面的路徑,問號后面的就是要傳遞過去的參數,s1、s2、s3都是參數名 ,等號后面的是它們的值,參數之間用 & 連起來;Uri的第二個參數 UriKind.RelativeOrAbsolute 表示前面的路徑是相對的還是絕對的,RelativeOrAbsolute 表示 可能是相對的也可能是絕對的。

?

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 //返回上一個頁面
19 this.NavigationService.GoBack();
20 }
21
22 //當該頁面是活動頁面時觸發
23 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
24 {
25 base.OnNavigatedTo(e);//調用基類的虛方法是應該的,但不是必須的
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 //當該頁面不是活動頁面時觸發
36 protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
37 {
38 base.OnNavigatedFrom(e);
39 MessageBox.Show("離開SecondPage");
40 }
41
42
43 //public void Get()
44 //{
45 ////不能這樣在自己寫的方法里調用NavigationContext.QueryString
46 // IDictionary<string, string> text = NavigationContext.QueryString;
47 //}
48
49 }
50 }

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

OnNavigatedTo方法會在當前頁面成為活動時被觸發,在這里,可以使用 NavigationContext.QueryString 來獲取從MainPage傳遞過來的參數。NavigationContext.QueryString 的類型是 IDictionary<string, string> ,就是 鍵/值 對。

當我們想回到MainPage 頁面時,可以通過 this.NavigationService.GoBack(); 實現

在離開SecondPage 頁面時,如果我們想在離開前再做最后一點事情,可以放在 OnNavigatedFrom 函數里,也是通過重寫基類的虛方法實現,當該頁面從活動變成不是活動頁面時觸發 protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)

?

?

轉載于:https://www.cnblogs.com/zouzf/archive/2012/03/19/2406224.html

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。