简体   繁体   English

WPF控件不在WinForms应用程序中的ElementHost中显示

[英]WPF control not displaying in ElementHost in WinForms app

I have a problem with a WPF control that I'm trying to host in an ElementHost in a WinForms app. 我有一个WPF控件的问题,我试图在WinForms应用程序中的ElementHost中托管。 The control is a lookless custom control that I originally developed in a separate test project, which was a WPF app. 该控件是一个无形的自定义控件,我最初是在一个单独的测试项目中开发的,它是一个WPF应用程序。 In there it obviously works fine, but in my WinForms app all I get is a blank grey box where the ElementHost is displayed. 在那里它显然工作正常,但在我的WinForms应用程序中,我得到的是一个空白的灰色框,其中显示ElementHost。

Here's my C# code for creating, populating, and adding the ElementHost to the parent Control: 这是我用于创建,填充和添加ElementHost到父Control的C#代码:

// This is my WPF control
m_TabHostPanel  = new TabHostPanel();
m_ElementHost  = new ElementHost
                 {
                     Child = m_TabHostPanel,
                     Dock = DockStyle.Top,
                     Height = 34
                 };
this.Controls.Add( m_ElementHost );

The parent control contains other WinForms controls that are added and removed at runtime, as needed. 父控件包含在运行时根据需要添加和删除的其他WinForms控件。 These are all hosted singly with their Dock set to DockStyle.Fill. 这些都是单独托管,其Dock设置为DockStyle.Fill。 Thus, every time I add one I send the ElementHost to the back of the Z-order to make sure it renders correctly: 因此,每次添加一个时,我都会将ElementHost发送到Z-order的后面,以确保它正确呈现:

m_ElementHost.SendToBack();

Thus, I know I'm not running into an airspace problem, or anything like that. 因此,我知道我没有遇到空域问题,或类似的问题。

The one thing I did wonder about is this: in the original project the styles for all my lookless controls were merged into the resource dictionary for the application in App.xaml, like this: 我想知道的一件事是这样的:在原始项目中,我所有无外观控件的样式被合并到App.xaml中的应用程序的资源字典中,如下所示:

<Application x:Class="WpfTestApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Application/UserInterface/DataTemplates/TabModelDataTemplate.xaml"/>
                <ResourceDictionary Source="Application/UserInterface/Styles/HoverablePressableButtonStyle.xaml"/>
                <ResourceDictionary Source="Application/UserInterface/Styles/MiniControlButtonStyle.xaml"/>
                <ResourceDictionary Source="Application/UserInterface/Styles/TabCloseButtonStyle.xaml"/>
                <ResourceDictionary Source="Application/UserInterface/Styles/TabScrollLeftButtonStyle.xaml"/>
                <ResourceDictionary Source="Application/UserInterface/Styles/TabScrollRightButtonStyle.xaml"/>
                <ResourceDictionary Source="Application/UserInterface/Styles/TabListDropDownButtonStyle.xaml"/>
                <ResourceDictionary Source="Application/UserInterface/Styles/TabHostComboBoxStyle.xaml"/>
                <ResourceDictionary Source="Application/UserInterface/Styles/TabButtonStyle.xaml"/>
                <ResourceDictionary Source="Application/UserInterface/Styles/TabHostPanelStyle.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

I've migrated App.xaml to my WinForms project, but the build action is set to Page. 我已将App.xaml迁移到我的WinForms项目,但构建操作设置为Page。 If I set it to ApplicationDefinition I get an error saying that the application has multiple entry points, which makes sense, but I'm wondering then if the styles, etc., are being picked up. 如果我将它设置为ApplicationDefinition,我得到一个错误,说应用程序有多个入口点,这是有道理的,但我想知道是否正在拾取样式等。 If not this might explain why I'm getting a blank grey rectangle where my control should be because, without these, there's nothing to define its look. 如果没有这可能解释为什么我得到一个空白的灰色矩形,我的控制应该是因为没有这些,没有什么可以定义它的外观。 So maybe the question is, how do I get these styles into my WinForms application so that my WPF controls can see them? 所以也许问题是,我如何将这些样式放入我的WinForms应用程序中,以便我的WPF控件可以看到它们?

I should probably also mention that this is running on .NET Fx 3.5. 我可能还应该提到这是在.NET Fx 3.5上运行的。

Anyway, for now I'm perplexed, so any help would be gratefully received. 无论如何,现在我很困惑,所以任何帮助都会感激不尽。

Many thanks! 非常感谢!

Bart 巴特

Thanks for replying, but I think you may misunderstand me: I'm trying to use a custom element, whose resources are normally in the Application object, not insert the application itself into the ElementHost. 感谢您的回复,但我想您可能会误解我:我正在尝试使用自定义元素,其资源通常位于Application对象中,而不是将应用程序本身插入到ElementHost中。

Fortunately, I've found an answer: 幸运的是,我找到了答案:

http://drwpf.com/blog/2007/10/05/managing-application-resources-when-wpf-is-hosted/ http://drwpf.com/blog/2007/10/05/managing-application-resources-when-wpf-is-hosted/

Short version: 精简版:

  • Set build action for App.xaml to Page 将App.xaml的构建操作设置为Page
  • In the code behind for App.xaml create a default constructor that just calls InitializeComponent() 在App.xaml背后的代码中,创建一个只调用InitializeComponent()的默认构造函数
  • When the WinForms app starts up, just create an instance of the App class. 当WinForms应用程序启动时,只需创建App类的实例。

And then it's all good: my WPF control appears as it should. 然后一切都很好:我的WPF控件显示应该如此。

Now, why is it I only find the answer after I've posted to StackOverflow? 现在,为什么我在发布到StackOverflow 之后才找到答案?

Thanks again, 再次感谢,

Bart 巴特

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

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