Search code examples
c#wpfwindowsformsintegration

Hosting a composite WPF application inside an Element Host


I'm hosting a WPF application inside an Element Host, and it has a WPF user control inside the application. At a certain moment inside the WPF application (since I know that the Application class will be null, I instantiate it like so :

    if (Application.Current == null)
    {
        // create the Application object
        new Application();

        // merge in your application resources
        Application.Current.Resources.MergedDictionaries.Add(
            Application.LoadComponent(
                new Uri("edit;component/Styles/Styles.xaml",
                UriKind.RelativeOrAbsolute)) as ResourceDictionary);
    }

The problem is that whenever I close the inner user control inside the WPF application, it causes for some reason the Resources to be inaccessible. It says that the Application object is null even through I did instantiate it at the start of the application. If I check the Application for null and then instantiate it it says that there is an active Application in the current AppDomain.


Solution

  • Okay I found out what is going on :

    Currently I'm hosting a WPF Control inside an Element Host and I've made the assumption that any control that is hosted inside the control will have its Application mapping from WPF to Win forms solved out. Alas, that is not the case. If you are having a composite control make sure you are having a separate Element Host Control for each of the WPF controls.