Navigation Service in App.xaml.cs C#/XAML

Any attempt to do the below in App.xaml.cs will result in “An object reference is required for the non-static field, method or property System.Windows.Navigation.NavigationService.Navigate(System.Uri)”

NavigationService.Navigate(new Uri("/Mainpage.xaml", UriKind.RelativeOrAbsolute));

Thus to ensure proper navigation within App.xaml.cs page, below is the right way.

(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/Mainpage.xaml", UriKind.RelativeOrAbsolute));

The reason for doing so is that each application has only one Frame and it exposes the NavigationService. Thus NavigationService is always accessible as there will definitely be an instance of it for any Windows Phone app.

Advertisements
This entry was posted in Windows Phone and tagged , , , . Bookmark the permalink.

3 Responses to Navigation Service in App.xaml.cs C#/XAML

  1. i tried this but the application will creash

    Like

  2. This is my code .but the app will crash
    (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri(“/PinLockScreen.xaml”, UriKind.RelativeOrAbsolute));

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s