As the title states, I’m calling a WPF form from a WinForm app, (with several classes on the call stack between) numerous times. Initially, the WPF only worked once. After some googling, I found that I had to add ShutdownMode.OnExplicitShutdown to the WPF application create.
Now, after adding that, upon exiting the first WPF form, control never returns from the ‘Run’ call and focus is given back to the WinForm dialog. If I exit that, I finally get control returned from the WPF run.
Winform:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm form = new MainForm();
Application.Run(form);
WPF pre-init:
application = new System.Windows.Application
{
ShutdownMode = System.Windows.ShutdownMode.OnExplicitShutdown
};
Calling the WPF form:
ConfirmForm form = new ConfirmForm();
application.Run(form);
What is going on here?:
Application.Run(in both WinForms and WPF) is a blocking call that runs the message loop and returns when the application is ready to exit.You should just call
Show(), which shows theFormorWindowand returns immediately.However, you will need to call
ElementHost.EnableModelessKeyboardInteropto tell WPF to use the WinForms message loop.