我有一个小问题,当我像这样在 WPF 中打开一个新窗口时:
private void button2_Click(object sender, RoutedEventArgs e)
{
var newWindow = new Main();
newWindow.Show();
}
如果我尝试在它结束时使用 Application.Current.Shutdown();,我的整个应用程序将关闭,而不仅仅是我的第一个初始窗口。所以我的问题是有没有办法在安全关闭前一个窗口的同时打开一个新窗口?
谢谢:)
请您参考如下方法:
我会做这样的事情:
//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
}
编辑:不过我还没有测试过这段代码..
