OrElse
OrElse

Reputation: 9959

WP8 Simple page navigation fails

I get the following exception while trying to navigate from one page to another.

  // Code to execute if a navigation fails
        private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            if (Debugger.IsAttached)
            {
                // A navigation has failed; break into the debugger
                Debugger.Break();
            }
        }

The structure of my app is as follows

enter image description here

And i try to navigate like so

public MainPage()
    {
        InitializeComponent();
        NavigationService.Navigate(new Uri("/Live.xaml", UriKind.Relative));
    }

What am i doing wrong here?

Upvotes: 0

Views: 1734

Answers (2)

Artem Zinnatullin
Artem Zinnatullin

Reputation: 4447

You can not access NavigationService from page constructor, first method, which will be called after constructor and which you are able to override and use NavigationService there is OnNavigateTo (As Anton Sizlikov answered). Or you can subscribe to Page.Loaded event (but it will happens after OnNavigateTo) and run your navigate code there.

Upvotes: 0

Anton Sizikov
Anton Sizikov

Reputation: 9230

Try to move navigation from constructor to OnNavigatedTo method.

    // Constructor
    public MainPage()
    {
        InitializeComponent();

    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        NavigationService.Navigate(new Uri("/Live.xaml", UriKind.Relative));
    }

Upvotes: 1

Related Questions