Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.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
C# 关闭上一个窗口时打开新窗口的方法_C#_Wpf - Fatal编程技术网

C# 关闭上一个窗口时打开新窗口的方法

C# 关闭上一个窗口时打开新窗口的方法,c#,wpf,C#,Wpf,我有一个小问题,当我在WPF中打开一个新窗口时,如下所示: private void button2_Click(object sender, RoutedEventArgs e) { var newWindow = new Main(); newWindow.Show(); } 如果我尝试使用Application.Current.Shutdown()在它结束时,我的整个应用程序关闭,而不仅仅是我的第一个初始窗口。所以我的问题是,有没

我有一个小问题,当我在WPF中打开一个新窗口时,如下所示:

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        var newWindow = new Main();
        newWindow.Show();

    }
如果我尝试使用
Application.Current.Shutdown()在它结束时,我的整个应用程序关闭,而不仅仅是我的第一个初始窗口。所以我的问题是,有没有办法在安全关闭前一个窗口的同时打开新窗口


谢谢:)

实现这一点的唯一方法是在外部运行程序(我将很快找到执行此操作的代码)。否则,当父应用程序关闭时,从主应用程序中创建的任何内容都将被销毁

启动新程序的代码:

System.Diagnostics.Process.Start("program.exe");

实现这一点的唯一方法是在外部运行程序(我将很快找到执行此操作的代码)。否则,当父应用程序关闭时,从主应用程序中创建的任何内容都将被销毁

启动新程序的代码:

System.Diagnostics.Process.Start("program.exe");

我会这样做:

//Somewhere in your class 
YourOtherForm otherForm = null;

//then, on the event handler
private void button2_Click(object sender, RoutedEventArgs e)  {
    if((otherForm.IsDisposed) || (null == otherForm)) {
         otherForm = new YourOtherForm();
         // or, this is an MDI or something
         // otherForm = new YourOtherForm(this);
         // assuming you have an extra constructor to pass the parent
    }
    otherForm.Show();
    this.Close(); // or this.Hide(); if it's the main form
}

编辑:我还没有测试过这段代码,但我会这样做:

//Somewhere in your class 
YourOtherForm otherForm = null;

//then, on the event handler
private void button2_Click(object sender, RoutedEventArgs e)  {
    if((otherForm.IsDisposed) || (null == otherForm)) {
         otherForm = new YourOtherForm();
         // or, this is an MDI or something
         // otherForm = new YourOtherForm(this);
         // assuming you have an extra constructor to pass the parent
    }
    otherForm.Show();
    this.Close(); // or this.Hide(); if it's the main form
}


编辑:我尚未测试此代码。

您需要在App.xaml中将关机模式更改为OnLastWindowClose。

您需要在App.xaml中将关机模式更改为OnLastWindowClose。

如果
this.Close()
,会发生什么?this.Close不是解决方案?application.shutdown将关闭应用程序!实际上,如果
this.Close()
在主窗体上执行该代码,它实际上可能会关闭整个窗体。。我已经有一段时间没有接触桌面开发了。。对不起,如果我说您正在尝试使用“上一个”和“下一个”创建多个类似windows的对话框,我是否正确?如果是这种情况,最好隐藏或更改内容。此外,您只需从新窗口关闭上一个窗口,类似“this.Parent.close()”的内容应该可以工作。O您好!这个.Close在打开和保持打开我的新窗口和关闭旧窗口的同时起作用!如果
this.Close()
,会发生什么?this.Close不是解决方案?application.shutdown将关闭应用程序!实际上,如果
this.Close()
在主窗体上执行该代码,它实际上可能会关闭整个窗体。。我已经有一段时间没有接触桌面开发了。。对不起,如果我说您正在尝试使用“上一个”和“下一个”创建多个类似windows的对话框,我是否正确?如果是这种情况,最好隐藏或更改内容。此外,您只需从新窗口关闭上一个窗口,类似“this.Parent.close()”的内容应该可以工作。O您好!这个.Close在打开和保持打开我的新窗口和关闭旧窗口的同时起作用!等一下。。这会打开整个程序的新实例吗?我不知道我是否理解此解决方案。您不必使用Process.Start关闭应用程序,但是,启动的应用程序将与创建它的应用程序分开运行。就像我提到的,一旦父母死了,所有的孩子都会死。但是,您的问题会引发一些其他问题……也许您可以编辑您的问题,并详细说明您希望实现的目标……也许可以找到另一种解决方案:D@MilkyWayJoe是的,这将打开该程序的一个全新实例。如前所述,无法关闭应用程序的父级,但仍能保留子级。因此,当我调用application.current.shutdown时,Justin是正确的。整个构建刚刚结束,它不再在运行过程中,而是此。Close使父级保持活动状态,但只会删除窗口!在我写评论的时候,我没有意识到它是主窗体(mdi父窗体或其他),正如我所说,我已经有一段时间没有接触任何桌面应用程序了……等等。。这会打开整个程序的新实例吗?我不知道我是否理解此解决方案。您不必使用Process.Start关闭应用程序,但是,启动的应用程序将与创建它的应用程序分开运行。就像我提到的,一旦父母死了,所有的孩子都会死。但是,您的问题会引发一些其他问题……也许您可以编辑您的问题,并详细说明您希望实现的目标……也许可以找到另一种解决方案:D@MilkyWayJoe是的,这将打开该程序的一个全新实例。如前所述,无法关闭应用程序的父级,但仍能保留子级。因此,当我调用application.current.shutdown时,Justin是正确的。整个构建刚刚结束,它不再在运行过程中,而是此。Close使父级保持活动状态,但只会删除窗口!在我写评论的时候,我没有意识到它是主要的形式(mdi parent或其他),就像我说的,我已经有一段时间没有接触任何桌面应用了。。