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

2023-09-03 00:32:51 作者:别调戏我丶小心我非礼你

我有一个WPF控件,我想在ElementHost的举办在WinForms应用程序的问题。控制是我最初在一个单独的测试项目,这是一个WPF应用程序一个无外观的自定义控件。在那里它显然能正常工作,但在我的WinForms应用程序我得到的是一个空白的灰色框,其中显示的ElementHost。

I have a problem with a WPF control that I'm trying to host in an ElementHost in a WinForms app. The control is a lookless custom control that I originally developed in a separate test project, which was a WPF app. In there it obviously works fine, but in my WinForms app all I get is a blank grey box where the ElementHost is displayed.

下面是我的C#code用于创建,填充,并添加ElementHost的到父控件:

Here's my C# code for creating, populating, and adding the ElementHost to the parent Control:

// 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 );

父控件包含的补充,并在运行时删除,如需要其他WinForms控件。这些都与他们的码头设置为DockStyle.Fill单独主持。因此,我每次添加一个我送ElementHost的在Z顺序的后面,以确保它使正确的:

The parent control contains other WinForms controls that are added and removed at runtime, as needed. These are all hosted singly with their Dock set to 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:

m_ElementHost.SendToBack();

因此​​,我知道我没有运行到空间问题,或类似的东西。

Thus, I know I'm not running into an airspace problem, or anything like that.

有一件事我做奇怪的是这样的:在原项目的样式为我所有的无外观对照组并入资源字典在App.xaml中的应用,像这样的:

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:

<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>

我已经迁移的App.xaml我的WinForms项目,但生成操作设置为页面。如果我将它设置为ApplicationDefinition我得到一个错误,指出应用程序具有多个入口点,这是有道理的,但我不知道那么如果款式等,都被拾起。如果不是这或许可以解释为什么我得到一个空白的灰色矩形在我的控制应该是因为,如果没有这些,还有什么来定义它的外观。因此,也许,问题是,我如何得到这些样式到我的WinForms应用程序,使我的WPF控件可以看到他们?

I've migrated App.xaml to my WinForms project, but the build action is set to 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. 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?

我应该还提到,这是在.NET Fx的3.5运行。

I should probably also mention that this is running on .NET Fx 3.5.

反正,现在我感到困惑,所以任何帮助将受到欢迎。

Anyway, for now I'm perplexed, so any help would be gratefully received.

非常感谢!

巴特

推荐答案

感谢您的回答,但我认为你可能误会我的意思:我试图用一个自定义元素,其资源通常是在应用程序对象,而不是插入应用程序本身进入的ElementHost

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.

幸运的是,我找到了答案:

Fortunately, I've found an answer:

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

短版:

设置生成操作的App.xaml中页 在背后的App.xaml创建只是调用的InitializeComponent一个默认的构造函数code() 当WinForms应用程序启动时,只需要创建一个应用程序类的一个实例。

然后这一切都很好。我的WPF控件显示,因为它应该

And then it's all good: my WPF control appears as it should.

现在,为什么我只找到答案的在的我已经贴到计算器?

Now, why is it I only find the answer after I've posted to StackOverflow?

再次感谢,

巴特