0

尝试从一页导航到另一页时出现以下异常。

  // 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();
            }
        }

我的应用程序的结构如下

在此处输入图像描述

我试着像这样导航

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

我在这里做错了什么?

4

2 回答 2

1

尝试将导航从构造函数移动到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));
    }
于 2013-03-10T13:23:01.743 回答
0

您无法从页面构造函数访问 NavigationService,第一个方法将在构造函数之后调用,并且您可以覆盖和使用 NavigationService 存在 OnNavigateTo (正如 Anton Sizlikov 回答的那样)。或者您可以订阅 Page.Loaded 事件(但它会在 OnNavigateTo 之后发生)并在那里运行您的导航代码。

于 2013-03-17T19:36:15.023 回答