我在尝试在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();
因此,我知道我没有遇到空域等问题。
我想知道的唯一一件事是:在原始项目中,所有没有外观的控件的样式都被合并到了应用程序的资源字典中的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上运行。无论如何,我现在感到困惑,所以非常感谢任何帮助。Bart