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

发布于 2024-10-05 02:00:39 字数 2846 浏览 5 评论 0原文

我尝试在 WinForms 应用程序的 ElementHost 中托管 WPF 控件时遇到问题。该控件是一个外观不好看的自定义控件,最初是我在一个单独的测试项目(一个 WPF 应用程序)中开发的。在那里它显然工作正常,但在我的 WinForms 应用程序中我得到的只是一个空白的灰色框,其中显示 ElementHost。

下面是我的 C# 代码,用于创建、填充 ElementHost 并将其添加到父控件:

// 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 控件,这些控件在运行时根据需要添加和删除。这些都是单独托管的,其 Dock 设置为 DockStyle.Fill。因此,每次我添加一个时,我都会将 ElementHost 发送到 Z 顺序的后面,以确保它正确渲染:

m_ElementHost.SendToBack();

因此,我知道我没有遇到空域问题或类似的问题。

我确实想知道的一件事是:在原始项目中,所有外观控件的样式都合并到 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>

我已将 App.xaml 迁移到我的 WinForms 项目,但构建操作设置为 Page。如果我将其设置为 ApplicationDefinition ,我会收到一条错误消息,指出该应用程序有多个入口点,这是有道理的,但我想知道是否正在选择样式等。如果不是,这可能可以解释为什么我的控件应该在的位置出现一个空白的灰色矩形,因为没有这些,就没有任何东西可以定义它的外观。所以也许问题是,如何将这些样式放入我的 WinForms 应用程序中,以便我的 WPF 控件可以看到它们?

我可能还应该提到它是在 .NET Fx 3.5 上运行的。

无论如何,现在我很困惑,所以任何帮助将不胜感激。

非常感谢!

巴特

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.

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

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.

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>

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?

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.

Many thanks!

Bart

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫负雪 2024-10-12 02:00:39

感谢您的回复,但我认为您可能会误解我:我正在尝试使用自定义元素,其资源通常位于 Application 对象中,而不是将应用程序本身插入到 ElementHost 中。

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

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

简短版本:

  • App.xaml 的构建操作设置为 Page
  • 在代码中将 后面为 App.xaml 创建一个默认构造函数,仅调用 InitializeComponent()
  • 当 WinForms 应用程序启动时,只需创建 App 类的实例。

然后一切都很好:我的 WPF 控件按其应有的方式显示。

现在,为什么我只有在发布到 StackOverflow 之后才能找到答案?

再次感谢

巴特

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/

Short version:

  • Set build action for App.xaml to Page
  • In the code behind for App.xaml create a default constructor that just calls InitializeComponent()
  • When the WinForms app starts up, just create an instance of the App class.

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?

Thanks again,

Bart

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文