WP7.5中的NavigationContext.QueryString.TryGetValue NullReferenceException

Wal*_*oni 2 c# wpf xaml windows-phone-7 windows-phone-7.1

我想在7.5之间传递一个字符串到页面

我已经阅读了一些指南,但是,我有一个NullReferenceException.

Page.xml.cs:

 var item = ListBoxTiers.SelectedItem as CTiers; 
 NavigationService.Navigate(new Uri("/DetailTiers.xaml?selectedItem=" + item.m_strCode, UriKind.Relative));
Run Code Online (Sandbox Code Playgroud)

如果我看一下调试器,我可以看到: "DetailTiers.xaml?selectedItem=C0000015"

在我的页面中,Page2.xms.cs:

    public Page2()
    {
        InitializeComponent();
        string strCodeTiers =   string.Empty;
        if (NavigationContext.QueryString.TryGetValue("selectedItem",out strCodeTiers)) // Exception here
        {

        }
Run Code Online (Sandbox Code Playgroud)

有谁知道我的错误在哪里?

Kev*_*sse 9

您不应该从构造函数中调用此代码,因为NavigationContext尚未初始化.请改用OnNavigatedTo事件:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    string strCodeTiers = string.Empty;

    if (NavigationContext.QueryString.TryGetValue("selectedItem",out strCodeTiers))
    {
         // Whatever
    }
}
Run Code Online (Sandbox Code Playgroud)