How the App works, it selects the first window started as the main window. Therefore, in your case, TestWindow will be selected as the main window. ShutdownMode in your code is somehow set to OnMainWindowClose . Therefore, after closing TestWindow , all child windows (including your MainWindow ) have Closing .
Thus, the problem here does not extend, but extends to the down closing event.
You should not create a window before your main window starts. Or, if you want, you can set ShutdownMode to OnLastWindowClose .
protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); Application.Current.ShutdownMode = ShutdownMode.OnLastWindowClose; TestWindow t = new TestWindow(); t.ShowDialog(); }
Or you can explicitly set MainWindow in the constructor of the main window:
public MainWindow(){ InitializeComponent(); Application.Current.MainWindow = this; }
However, if you use ShowDialog() , you cannot explicitly specify MainWindow . Since immediately after closing TestWindow (at that time it is still the main window), the entire application will be disconnected.
Edit : I canโt find a link to this, but it can be checked, and we can be sure of it, here is the debugging:
protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); new TestWindow();
source share