Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NavigationContext.QueryString.TryGetValue NullReferenceException in WP7.5

I would like to pass a string between to pages in 7.5

I have read some guides, but, I have a NullReferenceException .

Page.xml.cs :

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

If i look at the debugger, i can see : "DetailTiers.xaml?selectedItem=C0000015"

In my page , Page2.xms.cs:

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

        }

Anyone know where is my error ?

like image 452
Walter Fabio Simoni Avatar asked Sep 22 '26 16:09

Walter Fabio Simoni


1 Answers

You shouldn't call this code from the constructor, as the NavigationContext isn't initialized yet. Use the OnNavigatedTo event instead:

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

    if (NavigationContext.QueryString.TryGetValue("selectedItem",out strCodeTiers))
    {
         // Whatever
    }
}
like image 85
Kevin Gosse Avatar answered Sep 25 '26 06:09

Kevin Gosse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!