0

I need to navigate to a Page1.xaml from a Class1.cs.

I tried this way on MainPage.cs

public void test()
{
    NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
}

And access it in Class1.cs:

...
MainPage window = new MainPage();
window.test();
....

But I get:

"[System.NullReferenceException]
{System.NullReferenceException: Object reference not set to an
instance of an object."

in NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));

Already read this: How to navigate to a xaml page from class, but didn't work.

Any help how can I do this?

PS: Class1.cs and Page1.cs has different namespaces, if it matters.

Community
  • 1
  • 1

1 Answers1

1

You're not supposed to instantiate a page this way. Instead, you can use the Navigate method exposed by the PhoneApplicationFrame:

((PhoneApplicationFrame)Application.Current.RootVisual).Navigate(new Uri("/Page1.xaml", UriKind.Relative));
Kevin Gosse
  • 38,392
  • 3
  • 78
  • 94