C# 从页面加载WPF主窗口内容
我有一个主窗口和一个页面 我通过该代码将页面内容加载到主窗口中C# 从页面加载WPF主窗口内容,c#,wpf,C#,Wpf,我有一个主窗口和一个页面 我通过该代码将页面内容加载到主窗口中 NewPage abt = new NewPage(); this.Content = abt; 但是如何卸载页面(重新加载主窗口控件并关闭页面) 如果我使用相同的代码加载mainwindow内容,我会遇到运行时错误,我认为将页面加载到mainwindow内容不是一个好的解决方案,但如果需要,您可能会在更改之前获取当前状态并将其保存到某个属性(或其他类似xml文件的内容)。如下图所示: public partial class M
NewPage abt = new NewPage();
this.Content = abt;
但是如何卸载页面(重新加载主窗口控件并关闭页面)
如果我使用相同的代码加载mainwindow内容,我会遇到运行时错误,我认为将页面加载到mainwindow内容不是一个好的解决方案,但如果需要,您可能会在更改之前获取当前状态并将其保存到某个属性(或其他类似xml文件的内容)。如下图所示:
public partial class MainWindow()
{
FrameworkElement previousContent; // I believe Content property is of FrameworkElement type
public MainWindow()
{
...
}
...
public void ChangeContent()
{
previousContent = this.Content; // save state
NewPage abt = new NewPage();
this.Content = abt; // set new state
}
//And later You can restore this state by:
public void RestorPreviousContent()
{
this.Content = previousContent;
}
我这样做的方式是在XAML中有一个框架,如下所示:
<Frame Grid.RowSpan="4" Grid.ColumnSpan="3" x:Name="_NavigationFrame" NavigationUIVisibility="Hidden"/>
_NavigationFrame.Navigate(customPage);
//code to hide main page controls
_NavigationFrame.Navigate(null);
//code to make main page controls visible