我有一个WPF MVVM应用程序。我使用WindowManager
打开并显示一个视图。我的主shell视图模型如下,SomeMethod
显示ConsoleView
[Export(typeof(IShell))]
public sealed class ShellViewModel :
Conductor<IScreen>.Collection.OneActive, IShell, IDataErrorInfo
{
IWindowManager windowManager = null;
...
public SomeMethod()
{
...
dynamic eo = new ExpandoObject();
eo.WindowStartupLocation = WindowStartupLocation.CenterScreen;
ConsoleViewModel console = new ConsoleViewModel("Binary Table Compilation Output");
windowManager.ShowWindow(console, null, eo);
...
}
...
}
而ConsoleViewModel
是
[View(typeof(ConsoleView))]
public class ConsoleViewModel : Screen
{
...
}
问题是如果我关闭主应用程序,ConsoleView
不会关闭。问题是,如何在主应用程序/ shell执行时强制所有子窗口关闭?
答案 0 :(得分:5)
您可以向Window.Closed
event添加一个事件处理程序,并从那里关闭所有打开的Window
:
public void MainWindowClosed(object sender, EventArgs e)
{
foreach (Window window in Application.Current.Windows)
{
window.Close();
}
}