2014-07-11 26 views
2

我有一個WPF MVVM應用程序。我使用WindowManager打開並顯示一個veiw。我的主要外殼視圖模型是與顯示ConsoleView使用MVVM關閉所有Caliburn Micro的子窗口

[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); 
     ... 
    } 
    ... 
} 

SomeMethod如下與ConsoleViewModel

[View(typeof(ConsoleView))] 
public class ConsoleViewModel : Screen 
{ 
    ... 
} 

的問題是,如果我關機的主要應用,其ConsoleView不會關閉。問題是,如何在主應用程序/ shell執行時強制所有子窗口關閉?

回答

4

你可以從那裏添加一個事件處理程序Window.Closed event,並關閉所有打開的Window S:

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

好了,是的,但我認爲會有一個MVVM「卡利方式」? – MoonKnight

+0

使用Caliburn不*意味着你不能使用.NET類。 – Sheridan

+1

+1尋求幫助。讚賞。只是一個快速的。我很高興使用它,但有4個窗口顯示在Application.Current.Windows中,這些窗口不可見,我不知道它們是什麼!?主窗口位於第一個位置,但還有其他未實例化的窗口實例。任何想法爲什麼這些在那裏? – MoonKnight