C# 多个用户控件和程序集之间的共享资源字典
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/703026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shared resource dictionary between several user controls and across assemblies
提问by Dan Vogel
I have an assembly that contains several user controls. For this user controls assembly I want to have a resource dictionary. All the user controls within the assembly should be able to access the resource dictionary. Do I have to add
我有一个包含多个用户控件的程序集。对于这个用户控件程序集,我想要一个资源字典。程序集中的所有用户控件都应该能够访问资源字典。我必须添加吗
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
...
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
to every user control that should utilize the resource dictionary, or is there some way of putting it in one place and just referencing it?
应该使用资源字典的每个用户控件,或者是否有某种方法将它放在一个地方并只引用它?
can I then also reference it in my main application, or does that require a MergedDictionaries call as well?
然后我也可以在我的主应用程序中引用它,还是也需要一个 MergedDictionaries 调用?
Edit: the main application is in a separate project/assembly than the user controls.
编辑:主应用程序位于与用户控件不同的项目/程序集中。
采纳答案by holsee
is there some way of putting it in one place and just referencing it?
有没有办法将它放在一个地方并只引用它?
Put your merged dictionaries reference to your resource dictionaries into your 'App.xaml' file and it will be accessible throughout your application, you will need no futher reference.
将您对资源字典的合并字典引用放入您的“App.xaml”文件中,它可以在您的整个应用程序中访问,您将不需要进一步的参考。
can I then also reference it in my main application, or does that require a MergedDictionaries call as well?
然后我也可以在我的主应用程序中引用它,还是也需要一个 MergedDictionaries 调用?
No the scope of 'App.xaml' falls over the entire application, so this should work fine (does for me :) ).
'App.xaml' 的范围不属于整个应用程序,所以这应该可以正常工作(对我来说:))。
Update: How to reference resource dictionary stored items from user control.
更新:如何从用户控件引用资源字典存储的项目。
Within your main project add a reference to your user control library. Your user controls will be accessible and you can use them in you application as desired.
在您的主项目中添加对您的用户控件库的引用。您的用户控件将是可访问的,您可以根据需要在您的应用程序中使用它们。
The process of adding the resource dictionary reference in the App.xaml will mean that all controls can reference styles and data templates etc. defined in the resource dictionaries, so it it merely a matter of referencing them:
在 App.xaml 中添加资源字典引用的过程意味着所有控件都可以引用资源字典中定义的样式和数据模板等,因此只需引用它们:
e.g.
例如
Style="{StaticResource MyButtonStyle}"
This method works for both composite applications and regular WPF applications. Note that Visual Studio is no good at loading these styles from Linked XAML files (resource dictionary) but expression blend deals with it and will give the editor preview.
此方法适用于复合应用程序和常规 WPF 应用程序。请注意,Visual Studio 不擅长从链接的 XAML 文件(资源字典)加载这些样式,但表达式混合会处理它并提供编辑器预览。
回答by holsee
If the "App.xaml" approach doesn't work for you then you might be interessted in this discussion:
如果“App.xaml”方法对您不起作用,那么您可能会对以下讨论感兴趣:
Best Practices: How to handle shared resources in a modular application?
jbe
杰贝
回答by David Veeneman
You can use a Pack URLto reference resource dictionaries across assemblies. For example, in Prism projects (which have multiple modules, each in its own assembly), I store my common resource dictionaries in a ResourceDictionariesfolder in a project titled Common. Then I merge these dictionaries as needed into the app's modules by using markup similar to this in each module's XAML view:
您可以使用Pack URL来跨程序集引用资源字典。例如,在 Prism 项目(有多个模块,每个模块都在自己的程序集中)中,我将常用资源词典存储在名为Common的项目中的ResourceDictionaries文件夹中。然后我根据需要将这些字典合并到应用程序的模块中,方法是在每个模块的 XAML 视图中使用与此类似的标记:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Common;component/ResourceDictionaries/DemoDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
You can find further information here.
您可以在此处找到更多信息。

