Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 从用户控制页返回主窗口_Wpf - Fatal编程技术网

Wpf 从用户控制页返回主窗口

Wpf 从用户控制页返回主窗口,wpf,Wpf,谁能分享代码,让我回到主窗口而不打开新窗口 您可以使用此代码在页面之间导航: page1 page = new page1(); this.content = page; 但是,您不能使用以下代码导航回主窗口: MainWindow home = new MainWindow(); this.content = home; 关于如何在不打开新窗口的情况下返回主窗口,有什么建议吗?步骤1-通过构造函数使Page类引用主窗口 public partial class Page1 : Pa

谁能分享代码,让我回到主窗口而不打开新窗口

您可以使用此代码在页面之间导航:

page1 page = new page1();
this.content = page;
但是,您不能使用以下代码导航回主窗口:

MainWindow home = new MainWindow();
this.content = home;

关于如何在不打开新窗口的情况下返回主窗口,有什么建议吗?

步骤1-通过构造函数使Page类引用主窗口

    public partial class Page1 : Page
    {    
    private MainWindow mainWindow;

    public Page1(MainWindow mainWindow)
    {
        InitializeComponent();
        this.startWindow = startWindow; 

        //SET Height and width to match the content of the page if it differs from 
        //mainwindow
        Application.Current.MainWindow.Height = 450;
        Application.Current.MainWindow.Width = 450;   
    } 
第2步-在主窗口中,当您想将内容更改为第1页时,请创建一个方法来更改内容

public void ChangeContentToPage1Action(){
    Page1 page = new Page1(this);
    this.content = page;
 }
步骤3-在MainWindow中创建一个属性,该属性将记住正在显示的启动MainWindow内容。并在MainWindow中创建一个方法以返回默认内容

   public partial class MainWindow : Window
   {
    private object content;

    public MainWindow()
    {
        InitializeComponent();
        content = Content;
    }

 public void GoBackToStartPage()
        {
            Content = cont;
            //Sets the height and width of the application back to default if it differs from the page calling this method
            Application.Current.MainWindow.Height = 200;
            Application.Current.MainWindow.Width = 300;
        }
步骤4-从Page1类中,您现在可以通过mainWindow属性调用GoBackToStartPage,并将内容更改回默认值,如下所示

mainWindow.GoBackToStartPage();
这对我有帮助


我希望有人能用这个

是的,他们能。你能问一个具体的细节吗,或者我们只是猜测一下你的要求吗?好吧,要从一页导航到另一页,你可以使用代码home=newhomepage;这个。内容=家;但是当我想使用代码MainWindow main=newmainWindow返回主窗口时;this.content=main;它不起作用……有什么建议吗?我建议你编辑你的问题,并在那里提供所有必需的信息。要明确的是,您的评论并没有提供所有必需的信息。。。想想如果有人让你这么做,你需要知道的一切,这样会好一点。尝试阅读MSDN上的页面。