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

时间:2015-09-08 15:14:37

标签: c# wpf xaml navigation nullreferenceexception

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

我的问题是我创建了一个 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;
}

2 个答案:

答案 0 :(得分:0)

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

Frame rootFrame = Window.Current.Content as Frame;  

您应该在MasterPage的构造函数中添加以下代码行,其中“rootFrame”是您网页的属性。

答案 1 :(得分:0)

问题已解决

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

这里有链接,了解如何使用MasterPages以及如何处理这些页面中的点击:

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