在WPF应用程序中从窗口导航到页面时出错

时间:2013-11-10 05:23:55

标签: c# wpf xaml navigation

我读了数百个Q / A&博客相同,但无法解决我得到的错误。在我的WPF应用程序中,我需要从MainWindow.xaml导航到Page Register.xaml。我有以下代码:

        Register register = new Register();
        MainWindow.Navigate(register);

或者

this.NavigationService.Navigate(new Uri("Register.xaml ", UriKind.Relative));

它给了我错误 '.MainWindow'不包含'Navigate'的定义 要么 MainWindow'不包含'NavigationService'的定义

1 个答案:

答案 0 :(得分:0)

要打开page,您需要将其设置为frame

A page can be hosted from Window, NavigationWindow, Frame, or from a browser. To be hosted, a page can be:
  • 中的Window,NavigationWindow或Frame元素的直接子元素 XAML。

  • 实例化并设置为Window,NavigationWindow和Frame的Content属性的值。

  • 设置为NavigationWindow或Frame的Source属性的统一资源标识符(URI)源。

如果您想在MainWindow中使用它,您可以执行以下操作。

XAML

<Frame Name="contentFrame" />

C#代码隐藏

contentFrame.Content = new Page();

要在新窗口中打开它,您可以执行以下操作。

Page p = new Page();
p.Show();