Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullReferenceException when calling NavigationContext

In a Click function I use

NavigationService.Navigate(new Uri("/MainPage.xaml?day=" + this.week.SelectedIndex, UriKind.Relative));

to navigate to MainPage.xaml with a value which indicate the Panorama.DefaultItem,and in MainPage.xaml.cs I write like

if (this.NavigationContext.QueryString.ContainsKey("day"))
{
    schedule.DefaultItem = NavigationContext.QueryString["day"];
}

but a NullReferenceException comes out in

this.NavigationContext.QueryString.ContainsKey("day") 

and I don't know where the wrong is,I will be appreciate if somebody can help me with this problem...

like image 734
Yugy Avatar asked May 10 '12 01:05

Yugy


1 Answers

I bet you are accessing NavigationContext in the constructor. The framework hasn't filled that in yet at that point. Check it in OnNavigatedTo or any time after and you should be fine.

like image 90
Austin Thompson Avatar answered Nov 17 '22 06:11

Austin Thompson