C# WPF 如何正确关闭新窗口? [英] C# WPF How to close a new window correctly?
问题描述
我有一个简单的问题:
1- 创建一个新的 WPF 项目,其中有一个启动窗口:MainWindow.xaml
.
1- Create a new WPF project, there's startup window in it: MainWindow.xaml
.
2- 在这个项目中,创建一个新窗口 Window1.xaml
.
2- In this project, create a new window Window1.xaml
.
3- Windows1
的加载事件,让它关闭.
3- Windows1
's loaded event, let it close.
4- 将 Open
按钮放置到 MainWindow
.实现它的点击事件.
4- Put an Open
button to MainWindow
. Implement it's click event.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Close();
}
private void button_Click(object sender, RoutedEventArgs e)
{
Window1 w = new Window1();
w.Show();
}
当我启动这个应用程序时,VS2015的UI变成了DEBUGING MODE
,然后我点击窗口右上角的关闭按钮,VS2015的UI回到正常模式.
When I start this application, VS2015's UI became to the DEBUGING MODE
, then I click the close button on the right top corner of the Window, VS2015's UI back to normal mode.
现在,如果我启动应用程序,单击 Open
按钮,然后 Window1
将快速显示并关闭,但是,如果我单击右上角的关闭按钮MainWindow
,事情不同了:VS2015 不会回到正常模式而是停留在DEBUGING MODE
.所以对我来说,这意味着有什么东西挂在那里,我不知道它是什么.
Now, if I start application, click Open
button, then Window1
will show quickly and closed, but, if I click the close button on the right top corner of MainWindow
, things different: VS2015 will no go back to normal mode but stay in DEBUGING MODE
. so to me, that means something hang there and I don't know what is it.
有人知道如何解决这个问题吗?
Is there anyone know how to fix this problem?
推荐答案
在 App.xaml 集中:
In the App.xaml set:
ShutdownMode="OnMainWindowClose"
这必须解决问题.我建议阅读这个问题.
This must solve the problem. I recommend reading this question.
这篇关于C# WPF 如何正确关闭新窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!