在获取NavigationContext.QueryString时,在页面加载时获取NullReferenceException

时间:2012-09-07 16:35:47

标签: windows-phone-7

我使用MVVM模式开发我的Windows应用程序。当用户单击列表框usercontrol上的项目时。 viewmodel背后的代码将导航另一个页面。页面显示正确,但问题是当我想从Navigation上下文获取queryString时出现错误 - “NullReferenceException”。我检查了我的viewmodel上的uri已更正。有人会告诉我如何让它发挥作用吗?提前谢谢。

我在An error is occur when we use navigation to move other page引用以在App.xamls.cs页面上添加以下代码,因此viewModel页面可以导航到另一个页面。

    public static PhoneApplicationFrame CurrentRootVisual
    {
        get
        {
            return (App.Current.RootVisual as PhoneApplicationFrame);
        }
    }

    public static bool Navigate(Uri source)
    {
        if (CurrentRootVisual != null)
            return CurrentRootVisual.Navigate(source);

        return false;
    }

    public static void GoBack()
    {
        if (CurrentRootVisual != null)
            CurrentRootVisual.GoBack();
    }

viewModel上有代码导航页面:

 private void ShowCallPage()
        {
            if (m_CurrentQueue != null)

                App.Navigate(new Uri("/PivotPage1.xaml?id=" + m_CurrentQueue.callNumber, UriKind.Relative));

        }

我在另一页上遇到错误的代码:

 public PivotPage1()
    {
        InitializeComponent();
        MessageBox.Show(NavigationContext.QueryString.ContainsKey("id").ToString());            

    }

1 个答案:

答案 0 :(得分:1)

不要访问PivotPage1构造函数上的NavigationContext对象,而是使用Loaded事件。

相关问题