简体   繁体   English

在元素主机中托管复合WPF应用程序

[英]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. 我将WPF应用程序托管在Element Host中,并且在应用程序内部具有WPF用户控件。 At a certain moment inside the WPF application (since I know that the Application class will be null , I instantiate it like so : 在WPF应用程序内的某个时刻(因为我知道Application类将为null ,所以我将其实例化为:

    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. 问题是,每当我关闭WPF应用程序内部的内部用户控件时,由于某种原因,这将导致无法访问资源。 It says that the Application object is null even through I did instantiate it at the start of the application. 它说Application对象为null即使我在应用程序启动时实例化了它。 If I check the Application for null and then instantiate it it says that there is an active Application in the current AppDomain. 如果我检查Application是否为null ,然后实例化它,则说明当前AppDomain中有一个活动的Application。

Do you mean you want to host the whole application within ElementHost? 您是说要在ElementHost中托管整个应用程序吗? I think ElementHost is primarily designed to host WPF controls rather than whole applications. 我认为ElementHost的主要目的是托管WPF控件,而不是整个应用程序。

You may already have been around this block but having tried to get WPF controls working inside ElementHost using various techniques I settled on just keeping the Application object out of the equation and just referencing my resources within the user control itself (so ElementHost contains the UserControl and this references its resources; with no use of the Application object at all). 您可能已经解决了这个问题,但是尝试使用各种技术使WPF控件在ElementHost中工作,我决定将Application对象置于等式之外,并仅在用户控件本身中引用我的资源(因此ElementHost包含UserControl和它引用其资源;根本不使用Application对象)。

This article goes into detail of how to manage Application if you decide you need the Application object. 本文详细介绍了如果您决定需要Application对象,则如何管理Application。

To reference resources within your control you can use the following in your UserControl xaml: 要引用控件中的资源,可以在UserControl xaml中使用以下命令:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/MyAssembly.NameSpace;component/Resource/ResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
   </ResourceDictionary>
</UserControl.Resources>

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. 当前,我在元素主机中托管了WPF控件,并且我假设托管在控件中的任何控件都将解决从WPF到Win窗体的应用程序映射。 Alas, that is not the case. las,事实并非如此。 If you are having a composite control make sure you are having a separate Element Host Control for each of the WPF controls. 如果要使用复合控件,请确保每个WPF控件都具有单独的Element Host控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM