I used the MVVM pattern to develop my Windows app. When the user click on the item on my listbox usercontrol. The code behind of the viewmodel will navigate the another page. The page is shown correctly, but the problem is I got an error -” NullReferenceException” when I want to get the queryString from Navigation context. I checked the uri on my viewmodel is corrected. Would some one show me how to make it work? Thanks in advance.
I reference at An error is occur when we use navigation to move other page to add the following code on App.xamls.cs page, so the viewModel page can navigate another page.
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();
}
There is code on viewModel to navigate the page:
private void ShowCallPage()
{
if (m_CurrentQueue != null)
App.Navigate(new Uri("/PivotPage1.xaml?id=" + m_CurrentQueue.callNumber, UriKind.Relative));
}
There is the code I get error on the another page:
public PivotPage1()
{
InitializeComponent();
MessageBox.Show(NavigationContext.QueryString.ContainsKey("id").ToString());
}
Don’t access the NavigationContext object on the PivotPage1 constructor, use the Loaded event instead.