Closing the WPF Modal dialog box closes the application unexpectedly

I had a problem with the unexpected termination of my application when the modal dialog generated from the main program window closes normally. No unhandled exceptions are thrown, and none of the Closing or Closed events fires in the main application window.

Essentially, I have a main / shell window that runs in the application code using ShellWindow.Show (). Through the menu, the user can generate a custom opening dialog, this is a new window created and then shown using ShowDialog (the owner of the window is set in the window of the shell window).

When the dialog is closed (internally, when _modalDialogWindow.Close () is called), the application closes, while I expected the modal dialog to be closed.

Debugging the code indicates that ShellWindow is being unloaded from memory because the next line of code after _modalDialogWindow.Close () has dropped out of Application.Run () in the static program code.

If anyone has any ideas, I’m ready to try something.

+4
source share
2 answers

It seems that due to the MVVM / Ioc method, I am developing an application window, closing events are propagated further than necessary. I do not understand this!

However, setting Application.ShutDownMode to Explicit prevents the application from shutting down prematurely, and now I have the desired behavior.

Incidentally, the inclusion of all the exceptions suggested by declyclone did not lead to any exceptions that are thrown internally when the window closes.

+4
source

Do not create windows before creating the application, otherwise they will not be registered properly. They will not appear in Application.Current.Windows or Application.Current.MainWindow. Then, when you create your dialog box, your application will think that it is both MainWindow and the only window.

An example of what not to do:

 public partial class App : Application, ISingleInstanceApp { MyWindow win = new MyWindow(); //BAD! this is called inside new App(), but before the actual App constructor. [STAThread] public static void Main() { if (SingleInstance<App>.InitializeAsFirstInstance(Unique)) { var application = new App(); application.InitializeComponent(); application.Run(); // Allow single instance code to perform cleanup operations SingleInstance<App>.Cleanup(); } } 

I also had this problem, your answer helped me understand why.

+1
source

Source: https://habr.com/ru/post/1340760/


All Articles