win8外包公司——技术分享:参数传递

页面之间传递参数

windows phone 的参数传递和web 差不多。用“?”号传递 多个参数的时候用 “&”做分隔。

我接着昨天的项目继续添加一个FourPage.xaml

在昨天的ThreePage.xaml中添加两个 TextBlock控件分别在Text 中写上 “用户名",”密码"。 再添加两个TextBox控件,分别取名txtUserName,txtPassword

。接着添加一个按钮Button 取名toFourPage。然后双击按钮在 toFourPage 的单击事件中添加如下代码:

 

  1. private void toFourPage_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     //判断用户名,密码是否为空  
  4.     string username = this.txtUserName.Text;  
  5.     string password = this.txtPassword.Text;  
  6.   
  7.     if (username == "")  
  8.     {  
  9.         MessageBox.Show("用户名为空");  
  10.         return;  
  11.     }  
  12.     else if (password == "")  
  13.     {  
  14.         MessageBox.Show("你再怎么也瞎填点东西。");  
  15.         return;  
  16.     }  
  17.     else  
  18.     {  
  19.         this.NavigationService.Navigate(new Uri("/FourPage.xaml?UserName=" + username + "&Password=" + password,UriKind.Relative));  
  20.     }  
  21. }  
        private void toFourPage_Click(object sender, RoutedEventArgs e)
        {
            //判断用户名,密码是否为空
            string username = this.txtUserName.Text;
            string password = this.txtPassword.Text;

            if (username == "")
            {
                MessageBox.Show("用户名为空");
                return;
            }
            else if (password == "")
            {
                MessageBox.Show("你再怎么也瞎填点东西。");
                return;
            }
            else
            {
                this.NavigationService.Navigate(new Uri("/FourPage.xaml?UserName=" + username + "&Password=" + password,UriKind.Relative));
            }
        }

 

然后我们继续添在FourPage.xaml 中添加两个 TextBlock 用来存放 传递过来的参数。按F7进入代码页面。添加代码如下:

 

  1. //上一章讲的OnNavigatedTo事件。当从其他页过来的时候。  
  2. protected override void OnNavigatedTo(NavigationEventArgs e)  
  3. {  
  4.     base.OnNavigatedFrom(e);  
  5.   
  6.     this.UserName.Text = this.NavigationContext.QueryString["UserName"];  
  7.     this.Password.Text = this.NavigationContext.QueryString["Password"];  
  8. }  
 
        //上一章讲的OnNavigatedTo事件。当从其他页过来的时候。
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            this.UserName.Text = this.NavigationContext.QueryString["UserName"];
            this.Password.Text = this.NavigationContext.QueryString["Password"];
        }

OK 启动模拟器,先进到我们昨天添加的 页面三(ThreePgae.xaml)我在用户名中填写 任意值,继续在密码框中填写任意值点击登录。(注:因为这章主要是讲页面值的传递,以为密码框用的明文。)

OK 页面中最简单值的传递就 演示完了。

(写的不好 请见谅,有不对请留言告知我,免得误人子弟。)

代码下载 地址 http://download.csdn.net/download/gongkepop/6042391

ref http://blog.csdn.net/gongkepop/article/details/10470775

posted @ 2013-12-08 18:20  Unity外包团队  阅读(330)  评论(0编辑  收藏  举报