[英]WPF mainwindow content load from a page
I have one MainWindow and one page I load the page content into the mainwindow by that code我有一个 MainWindow 和一个页面,我通过该代码将页面内容加载到主窗口中
NewPage abt = new NewPage();
this.Content = abt;
but how can I unload the page (reload the mainwindow control and close the page) if I use the same code to load mainwindow content I get a runtime error但是如果我使用相同的代码加载主窗口内容,我如何卸载页面(重新加载主窗口控件并关闭页面)我收到运行时错误
The way I have done this is to have a Frame in the XAML like so:我这样做的方法是在 XAML 中有一个框架,如下所示:
<Frame Grid.RowSpan="4" Grid.ColumnSpan="3" x:Name="_NavigationFrame" NavigationUIVisibility="Hidden"/>
And then I can set a page and unload a page with this:然后我可以设置一个页面并卸载一个页面:
_NavigationFrame.Navigate(customPage);
//code to hide main page controls
_NavigationFrame.Navigate(null);
//code to make main page controls visible
I don't think loading page into MainWindow content is a good solution, but if You need it You could probably get current state and save it to some property(or some other thing like xml file) before changing.我不认为将页面加载到 MainWindow 内容是一个好的解决方案,但是如果您需要它,您可能会在更改之前获取当前状态并将其保存到某个属性(或其他一些东西,例如 xml 文件)。 Like Below:如下图:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.