簡體   English   中英

WPF控件不在WinForms應用程序中的ElementHost中顯示

[英]WPF control not displaying in ElementHost in WinForms app

我有一個WPF控件的問題,我試圖在WinForms應用程序中的ElementHost中托管。 該控件是一個無形的自定義控件,我最初是在一個單獨的測試項目中開發的,它是一個WPF應用程序。 在那里它顯然工作正常,但在我的WinForms應用程序中,我得到的是一個空白的灰色框,其中顯示ElementHost。

這是我用於創建,填充和添加ElementHost到父Control的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-order的后面,以確保它正確呈現:

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上運行的。

無論如何,現在我很困惑,所以任何幫助都會感激不盡。

非常感謝!

巴特

感謝您的回復,但我想您可能會誤解我:我正在嘗試使用自定義元素,其資源通常位於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 之后才找到答案?

再次感謝,

巴特

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM