使用MVVM关闭Caliburn Micro的所有子窗口

时间:2014-07-11 09:22:40

标签: wpf mvvm caliburn.micro

我有一个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执行时强制所有子窗口关闭?

1 个答案:

答案 0 :(得分:5)

您可以向Window.Closed event添加一个事件处理程序,并从那里关闭所有打开的Window

public void MainWindowClosed(object sender, EventArgs e)
{
    foreach (Window window in Application.Current.Windows)
    {
        window.Close();
    }
}