2010-12-01 142 views
9

我有一个WPF控件的问题,我试图在WinForms应用程序的ElementHost中托管。该控件是我最初在一个单独的测试项目中开发的一个看不见的自定义控件,它是一个WPF应用程序。在那里它显然工作正常,但在我的WinForms应用程序中,我所得到的是显示ElementHost的空白灰色框。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上运行的。

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

非常感谢!

巴特

回答

10

感谢回答,但我想你可能误会我的意思:我试图使用自定义元素,它的资源通常是在应用程序对象,而不是插入应用程序本身到ElementHost的。

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

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

短版:为App.xaml中

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

然后它都很好:我的WPF控件显示为它应该。

现在,为什么我只能在之后找到答案我已经发布到StackOverflow?再次

感谢,

巴特

+0

你如何创建App类的新实例? – Prof 2015-07-29 01:27:17

相关问题