wpf Mainwindow关闭时如何关闭所有窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18243260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to close all the windows when Mainwindow closes
提问by Zoya Sheikh
I am working on WPF. There is Mainwindow which opens as application starts. There are two buttons within this window. Each open a new window. e.g There are Add and update buttons. Add button opens a Add-Item window on its click event call and similarly update opens it's window "Update-Item". If i close Mainwindow these two windows "Add-Item" and "Update-Item" remains open. I want that if i close Mainwindow, these other two windows should also close with it.
我正在研究 WPF。主窗口在应用程序启动时打开。此窗口中有两个按钮。每个打开一个新窗口。例如,有添加和更新按钮。添加按钮在其单击事件调用上打开一个添加项目窗口,类似地更新打开它的窗口“更新项目”。如果我关闭 Mainwindow,这两个窗口“Add-Item”和“Update-Item”保持打开状态。我希望如果我关闭 Mainwindow,其他两个窗口也应该随之关闭。
app.current.shutdown
app.current.shutdown is used mostly. My Question is: Where i have to post this code line in my program, in mainwindow or in App.config. Should i have to call any event or function in it's response too?
app.current.shutdown 最常用。我的问题是:我必须在我的程序、主窗口或 App.config 中发布此代码行。我也应该在它的响应中调用任何事件或函数吗?
回答by H.B.
Set Application.MainWindowto the instance of your main window and make sure the Application.ShutdownModeis OnMainWindowClose.
设置Application.MainWindow为主窗口的实例并确保Application.ShutdownMode是OnMainWindowClose.
Also if you do not want the whole application to shut down: Make the MainWindowthe Ownerof the child windows. (This has other side effects)
此外,如果你不希望整个应用程序关闭:使MainWindow该Owner子窗口。(这还有其他副作用)
回答by J45
You can also set ShutdownMode in the App.xaml file:
您还可以在 App.xaml 文件中设置 ShutdownMode:
<Application x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
StartupUri="MainWindow.xaml"
ShutdownMode="OnMainWindowClose">
<Application.Resources>
</Application.Resources>
</Application>
回答by Forest Kunecke
I think the solution is to set the Ownerproperty of each of the sub-windows to your main window. That way, any window action performed on your main window is also performed on all of the other windows:
我认为解决方案是将Owner每个子窗口的属性设置为您的主窗口。这样,在主窗口上执行的任何窗口操作也会在所有其他窗口上执行:
http://msdn.microsoft.com/en-us/library/system.windows.window.owner.aspx
http://msdn.microsoft.com/en-us/library/system.windows.window.owner.aspx
回答by Mehrad
Since in the first glance it's quite hard to figure out the right way of doing what our friends advised I proceed with a little example (best practice) code.
因为乍一看很难找出正确的方法来做我们朋友的建议,所以我继续使用一些示例(最佳实践)代码。
This is what Josh Smithshows how your App.xaml.csshould look like.
这就是Josh Smith展示的App.xaml.cs应该是什么样子。
namespace MyApplication
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
static App()
{
// Ensure the current culture passed into bindings is the OS culture.
// By default, WPF uses en-US as the culture, regardless of the system settings.
//
FrameworkElement.LanguageProperty.OverrideMetadata(
typeof(FrameworkElement),
new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
}
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var window = new MainWindow();
// To ensure all the other Views of a type Window get closed properly.
ShutdownMode = ShutdownMode.OnMainWindowClose;
// Create the ViewModel which the main window binds.
var viewModel = new MainWindowViewModel();
// When the ViewModel asks to be closed,
// close the window.
EventHandler handler = null;
handler = delegate
{
viewModel.RequestClose -= handler;
window.Close();
};
viewModel.RequestClose += handler;
// Allow all controls in the window to bind to the ViewModel by
// setting the DataContext, which propagates down the element tree.
window.DataContext = viewModel;
window.Show();
}
}
}
Again, at the end of the day, it's up to you how you would want to layout your MVVM application.
同样,在一天结束时,您希望如何布局 MVVM 应用程序取决于您。

