我有一个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()似乎应该有效,但是从哪里可以得到现有字典的实例呢?
发布于 2009-02-23 21:43:32
假设你知道你需要什么资源(听起来像是你需要的),你应该能够自己“注入”它们。类似于:
var wpfControl = new ...;
wpfControl.Resources.Add(...);
elementHost.Child = wpfControl;在您的问题中,您提到了控制库中有现有的资源字典。如果是这样的话,你可以这样做:
var wpfControl = new ...;
wpfControl.Resources.MergedDictionaries.Add(/* instance of existing dictionary */);
elementHost.Child = wpfControl;发布于 2009-07-21 19:05:51
为了加载嵌入到程序集中的资源字典,我使用了以下代码片段在运行时加载它们:
//using System.Windows
ResourceDictionary dict = new ResourceDictionary();
dict.Source = new Uri("MyResourceDictionary.xaml", UriKind.Relative);
Application.Current.Resources.MergedDictionaries.Add(dict);这也适用于在可执行目录中加载字典。
发布于 2015-09-08 13:29:00
假设您将样式/模板/资源划分为多个文件( Resources1.xaml和Resources2.xaml ),则可以将它们聚合到单个资源字典(AllResources.xaml)中。可以轻松地将资源字典添加到控件的xaml文件中。参见下面的示例。
Page (!) Set Resources1.xaml、Resources2.xaml和AllResources.xaml生成操作
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>https://stackoverflow.com/questions/579460
复制相似问题