【发布时间】:2015-09-08 15:14:37
【问题描述】:
我正在制作一个 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;
}
【问题讨论】:
标签: c# wpf xaml navigation nullreferenceexception