1.使用?进行传递值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace PhoneApp4
{
public partial class MainPage : PhoneApplicationPage
{
// 构造函数
public MainPage()
{
InitializeComponent();
}
//按钮触发的方法
private void button1_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Welcome.xaml?name=admin", UriKind.Relative));
}
//页面跳转时候触发的方法
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
{
base.OnNavigatingFrom(e);
if (MessageBox.Show("您确定要跳转到下个页面吗?", "警告提示", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
{
e.Cancel = true;
}
}
}
}
2.在跳转到的页面进行接收受值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace PhoneApp4
{
public partial class Welcome : PhoneApplicationPage
{
public Welcome()
{
InitializeComponent();
}
//进入页面时调用的方法
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
//判断一下前台是否传来了值
if (NavigationContext.QueryString.ContainsKey("name"))
{
//使用queryString方法得到name的值,然后传给textBlock
textBlock1.Text = NavigationContext.QueryString["name"];
}else
MessageBox.Show("不存在这个键值");
}
}
}