从另一个页面调用导航方法 - XAML

Call a navigation method from another page - XAML

我正在制作一个 Windows 商店应用程序,我正在尝试创建一个 header 带有一些有助于在页面之间导航的按钮...

我的问题是我创建了一个 MasterPage.xaml(每个页面都使用它,所以我不需要每次都复制和粘贴样式) 并在里面创建了一些按钮和它们的点击事件。

private void HomePage_Click(object sender, RoutedEventArgs e)
{
    this.Frame.Navigate(typeof(Pages.HomePage));  
}

当我尝试从另一个页面单击按钮时,将出现 NullReferenceException 并检查 this.Frame 的值说它是 null.

如何使用从另一个页面调用的导航方法?

我试过在网上搜索,但找不到类似的东西...

编辑

App.xaml.cs 中,我不得不更改一些值以制作 MasterPage...

我的代码:

var rootFrame = Window.Current.Content as MasterPage;
if (rootFrame == null)
{
   rootFrame = new MasterPage();
   if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
   {
      // TODO: Load state from previously suspended application
   }
   Window.Current.Content = rootFrame;
}

获取当前帧的最佳方法是:

Frame rootFrame = Window.Current.Content as Frame;  

您应该在 MasterPage 的构造函数中添加这行代码,其中 "rootFrame" 是您页面的 属性。

问题已解决

网上冲浪我找到了解决方案。

在此处 link 查看如何使用 MasterPages 以及如何处理来自这些页面的点击:

http://loekvandenouweland.com/content/choosing-a-specific-master-page-for-each-windows-store-app-page