zoukankan      html  css  js  c++  java
  • 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)

  • 相关阅读:
    冒泡排序-用函数写
    c#语言基础
    c#小知识点
    令人头疼的冒泡排序
    字符串 与函数
    数组 冒泡排序 打印菱形 随机生成不同的数
    if语句练习
    运算符练习
    类型转换
    C#初学
  • 原文地址:https://www.cnblogs.com/zouzf/p/2406224.html
Copyright © 2011-2022 走看看