从嵌入在 WinForms 中的 WPF 控件获取 Generic.xaml [英] Getting Generic.xaml working from WPF control embedded in WinForms
问题描述
我有一个嵌入在 Windows 窗体中的 WPF 控件,它工作正常.但是我在 Themes/Generic.xaml 中拥有 WPF 项目的所有样式.当我将 WPF 控件嵌入到 Windows 窗体 ElementHost
控件中时,不会选取这些样式.
I have a WPF control embedded in a Windows Form, which works ok. But I have all my styles for the WPF project inside Themes/Generic.xaml. When I embed the WPF control inside a Windows Forms ElementHost
control, these styles are not picked up.
我是否需要更改 WPF 视图以在其资源部分中显式包含样式,还是仍应自动选取 generic.xaml 文件?
Do I need to change the WPF view to explicitly include the styles in its Resources section, or should the generic.xaml file be picked up automatically still?
推荐答案
Themes/Generic.xaml 是否在不同的程序集中?
Is Themes/Generic.xaml in a different assembly?
问题是应用程序没有找到定义样式的 ResourceDictionary.
The problem is that the application is not finding the ResourceDictionary where the styles are defined.
您可以做的是创建一个像这样的静态 SharedDictionaryManager 类:
What you can do is to create a static SharedDictionaryManager class like this one:
public static class SharedDictionaryManager
{
private static ResourceDictionary _sharedDictionary;
internal static ResourceDictionary SharedDictionary
{
get
{
if (_sharedDictionary == null)
{
System.Uri resourceLocater =
new System.Uri("/assembly;component/Themes/Generic.xaml",
System.UriKind.Relative);
_sharedDictionary =
(ResourceDictionary)Application.LoadComponent(resourceLocater);
}
return _sharedDictionary;
}
}
}
当你创建 HostControl 时,你添加资源字典来做控制:
And when you create the HostControl you add the resource dictionary do the control:
ctrl.Resources.MergedDictionaries.Add(SharedDictionaryManager.SharedDictionary);
如果您有多个资源字典,您可以为每个资源字典设置一个这样的属性,或者您可以拥有一个加载所有资源字典然后合并所有字典的属性.
If you have more than one resource dictionary you can have one property like this for each one or you can have a property that loads all the resource dictionaries and then merge all dictionaries.
现在,应该会自动选择您需要的样式.
Now, the style you need should be automatically picked.
这篇关于从嵌入在 WinForms 中的 WPF 控件获取 Generic.xaml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!