我尝试在 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 上运行的。
无论如何,现在我很困惑,所以任何帮助都将不胜感激。
非常感谢!
巴特