简体   繁体   English

WP8简单页面导航失败

[英]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 我的应用程序的结构如下

在此处输入图片说明

And i try to navigate like so 我尝试像这样导航

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

What am i doing wrong here? 我在这里做错了什么?

Try to move navigation from constructor to OnNavigatedTo method. 尝试将导航从构造函数移动到OnNavigatedTo方法。

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

    }

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

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). 您无法从页面构造函数的第一个方法访问NavigationService,该方法将在构造函数之后调用,并且您可以覆盖和使用NavigationService的是OnNavigateTo(正如Anton Sizlikov回答的那样)。 Or you can subscribe to Page.Loaded event (but it will happens after OnNavigateTo) and run your navigate code there. 或者,您可以订阅Page.Loaded事件(但它将在OnNavigateTo之后发生)并在那里运行导航代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM