
页面之间传递参数
windows phone 的参数传递和web 差不多。用“?”号传递 多个参数的时候用 “&”做分隔。
我接着昨天的项目继续添加一个FourPage.xaml
在昨天的ThreePage.xaml中添加两个 TextBlock控件分别在Text 中写上 “用户名",”密码"。 再添加两个TextBox控件,分别取名txtUserName,txtPassword
。接着添加一个按钮Button 取名toFourPage。然后双击按钮在 toFourPage 的单击事件中添加如下代码:
- 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));
- }
- }
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进入代码页面。添加代码如下:
- //上一章讲的OnNavigatedTo事件。当从其他页过来的时候。
- protected override void OnNavigatedTo(NavigationEventArgs e)
- {
- base.OnNavigatedFrom(e);
- this.UserName.Text = this.NavigationContext.QueryString["UserName"];
- this.Password.Text = this.NavigationContext.QueryString["Password"];
- }
//上一章讲的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 页面中最简单值的传递就 演示完了。
(写的不好 请见谅,有不对请留言告知我,免得误人子弟。)