导航窗口手机无法请求查询?

时间:2014-05-12 03:33:03

标签: c# xaml windows-phone

我在页面usercontrol中有一个page1.xaml并导航到page2.xaml

usercontrol中的代码是:

private void lbDataTopic_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri(string.Format("/PageTest.xaml?p=2"), UriKind.Relative));
}

我想在p

中请求查询字符串page2.xaml
public PageTest()
{
        InitializeComponent();
        try
        {
            if (!NavigationContext.QueryString.ContainsKey("p"))
            {
                MessageBox.Show(NavigationContext.QueryString["p"].ToString());
            }
            else
            {
                MessageBox.Show("error");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

但是当我正在运行程序时,它会显示错误

  

System.nullrefereceException:对象引用未设置为对象的实例"

1 个答案:

答案 0 :(得分:0)

您应该在OnNavigatedTo方法中获取QueryString,就像:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (NavigationContext.QueryString.ContainsKey("p"))
        {
            MessageBox.Show(NavigationContext.QueryString["p"]);
        }
        else
        {
            MessageBox.Show("error");
        }
    }