I found some rather strange behavior...
Here is how to replicate the problem:
private void Application_Startup(object sender, StartupEventArgs e)
"Form1 f1 = new Form1(); f1.ShowDialog();
Window1 w1 = new Window1(); w1.Show();
So, essentially what I do is create a WPF application that displays a winform. Then, the winform displays a WPF Window.
The first time the WPF window is displayed, everything works fine. If it is closed, and then re-opened, I get the exception! I can also open up multiple WPF windows by clicking the button multiple times. Once the last one is closed, however, I can never open another one again without the exception...
I have also tried the various tips suggested at http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/442782b1-00a1-4e2e-9cc6-ae99b6699126/ but those don't help.
Why is this happening?
Say, you want to use Windows Forms' ListBox control in your WPF application, you simply have to add the below code inside your Grid tag in XAML. The WindowsFormsHost tag is used to host Windows Forms control and wf:ListBox tag represents the Windows Forms ListBox.
Windows forms are poor in providing consistency. WPF provides more consistency across the applications. In Windows forms, UI has been designed with the help of the language of business logic code. In WPF, it uses the XAML as a markup language to design the UI part of the application.
In the main section you should see "WPF Custom Control Library" click on it and then name it what you would like and click ok. Add a Window(WPF) control to the project, this window would be the WPF window that you want to open. Then from the WinForm, open it like so: var wpfwindow = new WPFWindow.
It is simple to use WinForms as controls can be used easily, it's less time-consuming and less tricky as compared to WPF. It has an inbuilt story boarding feature and animation model, it has the ability to use business objects in UI declaratively. It does not provide such features.
The default Wpf applicationshutdown behavior (specified on the "Application" tab in project properties in VS or using the ShutdownMode attribute in Application.Xaml) is "On Last Window Close". This means that when you close the Wpf window you create, Wpf shuts down the application framework so any subsequent window creations will throw an exception.
You can avoid this problem by setting the Shutdown mode to "On explicit shutdown" ("OnExplicitShutdown" in Xaml). You will then need to manually call Application.Current.Shutdown explicitly when you want the app to terminate (e.g. when the winforms form is closed).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With