Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net WPF控件未显示在WinForms应用程序的ElementHost中_.net_Wpf_Winforms_Xaml_Elementhost - Fatal编程技术网

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

.net WPF控件未显示在WinForms应用程序的ElementHost中,.net,wpf,winforms,xaml,elementhost,.net,Wpf,Winforms,Xaml,Elementhost,我试图在WinForms应用程序的ElementHost中托管WPF控件时遇到问题。该控件是一个无外观的自定义控件,我最初是在一个单独的测试项目中开发的,该项目是一个WPF应用程序。在那里,它显然工作正常,但在我的WinForms应用程序中,我得到的只是一个显示ElementHost的灰色空白框 下面是我创建、填充ElementHost并将其添加到父控件的C#代码: // This is my WPF control m_TabHostPanel = new TabHostPanel(); m

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

下面是我创建、填充ElementHost并将其添加到父控件的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 );
父控件包含在运行时根据需要添加和删除的其他WinForms控件。这些都是单独托管的,其Dock设置为DockStyle.Fill。因此,每次添加一个元素时,我都会将ElementHost发送到Z顺序的后面,以确保其正确呈现:

m_ElementHost.SendToBack();
因此,我知道我没有遇到空域问题,或者类似的问题

我想知道的一件事是:在最初的项目中,我所有的lookless控件的样式都被合并到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项目,但构建操作设置为第页。如果我将其设置为ApplicationDefinition,我会得到一个错误,说明应用程序有多个入口点,这是有道理的,但我想知道是否正在提取样式等。如果不是,这也许可以解释为什么我得到了一个空白的灰色矩形,而我的控件应该在那里,因为没有这些,就没有什么可以定义它的外观。因此,问题可能是,如何将这些样式输入到WinForms应用程序中,以便WPF控件可以看到它们

我可能还应该提到,它是在.NETFX3.5上运行的

无论如何,现在我很困惑,所以任何帮助都会得到感激

非常感谢


Bart

感谢您的回复,但我想您可能误解了我的意思:我试图使用一个自定义元素,其资源通常位于Application对象中,而不是将应用程序本身插入ElementHost

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

简短版本:

  • 将App.xaml的生成操作设置为第页
  • 在App.xaml的代码隐藏中,创建一个只调用InitializeComponent()的默认构造函数
  • WinForms应用程序启动时,只需创建应用程序类的实例
然后一切都很好:我的WPF控件看起来应该是这样的

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

再次感谢


Bart

如何创建应用程序类的新实例?只需重新创建即可。