![](/img/trans.png)
[英]Creating a form from a WinForms Control hosted within WPF — Owner is not set
[英]Loading/Using Resource Dictionaries from a WinForms hosted WPF control
我有一個Windows窗體應用程序需要在運行時托管WPF控件。 我有基本的托管和交互完成(使用ElementHost控件),一切正常,直到我嘗試做一些需要WPF控件來使用定義的一些自定義資源字典。 (WPF控件及其所有資源字典都在同一個WPF控件庫DLL中定義。)
一旦發生這種情況,我就會收到一堆看起來像這樣的錯誤:
System.Windows.ResourceDictionary Warning: 9 : Resource not found; ResourceKey='DocumentHeaderInterestStyle'
我找到了一個引用 ( 鏈接因歸檔而顯示為死 , 這 可能與最初引用的文章相同)。 談到這一點,但似乎文章正在接近WPF方面的內容,但我真的不想對WPF控件進行更改,因為一切都在獨立的WPF應用程序中運行。
如果實現這一目標的唯一方法是在WPF端進行更改,我可以進行這些更改(我不負責WPF控件庫,但是那個也適用於同一家公司的人,所以這不是問題其他而不是花時間去做這些改變。)但是我希望能在WinForms方面做些什么才能讓它發揮作用。
WPF控件庫在項目中定義了一個名為“Default.xaml”的資源字典文件,其中包含以下屬性:
構建操作:頁面復制到輸出目錄:不要復制自定義工具:MSBuild:編譯
獨立的WPF應用程序在其App.xaml文件中包含以下條目:
<ResourceDictionary x:Uid="ResourceDictionary_1">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Uid="ResourceDictionary_2" Source="/SmartClient.Infrastructure;component/Themes\Default.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
似乎控制庫應該已經知道如何獲取其資源。 使用Resources.MergedDictionaries.Add()似乎應該可以工作,但是我從哪里獲得現有字典的實例?
假設你知道你需要什么資源(聽起來像你這樣做),你應該能夠自己“注入”它們。 就像是:
var wpfControl = new ...;
wpfControl.Resources.Add(...);
elementHost.Child = wpfControl;
在您的問題中,您提到控件庫中存在現有的資源字典。 如果是這樣,你可以這樣做:
var wpfControl = new ...;
wpfControl.Resources.MergedDictionaries.Add(/* instance of existing dictionary */);
elementHost.Child = wpfControl;
為了加載嵌入到程序集中的資源字典,我在運行時使用了以下代碼片段來加載它們:
//using System.Windows
ResourceDictionary dict = new ResourceDictionary();
dict.Source = new Uri("MyResourceDictionary.xaml", UriKind.Relative);
Application.Current.Resources.MergedDictionaries.Add(dict);
這也適用於在可執行文件目錄中加載字典。
假設您將樣式/模板/資源分成許多文件Resources1.xaml
和Resources2.xaml
您可以將它們聚合到單個資源字典( AllResources.xaml
)中。 資源字典可以輕松添加到控件的xaml文件中的控件中。 請參閱下面的示例。
(!)將Resources1.xaml
, Resources2.xaml
和AllResources.xaml
構建操作設置為Page
Resources1.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ControlTemplate x:Key="ScrollViewerControlTemplate" TargetType="{x:Type ScrollViewer}">
...
</ControlTemplate>
<LinearGradientBrush x:Key="VerticalScrollBarBackground" EndPoint="1,0" StartPoint="0,0">
...
</LinearGradientBrush>
<Style x:Key="StyleA" TargetType="{x:Type ScrollBar}">
...
</Style>
</ResourceDictionary>
Resources2.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="StyleB" TargetType="{x:Type ScrollBar}">
...
</Style>
<Style x:Key="StyleC" TargetType="{x:Type TextBlock}">
...
</Style>
</ResourceDictionary>
AllResources.xaml
將資源字典源添加為AllResources.xaml的 相對路徑
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources1.xaml" />
<ResourceDictionary Source="Resources2.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
最后在你的UserControl中
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/projectName;component/PathToTheFileRelativeToProjectRootDirectory/AllResources.xaml
<converters:StringToUpperCaseConverter x:Key="StringToUpperCaseConverter" />
<converters:LocalizationEntryToStringCaseConverter x:Key="LocalizationEntryToStringCaseConverter" />
</ResourceDictionary>
</UserControl.Resources>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.