.net 单独程序集中的 ResourceDictionary
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/338056/
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
ResourceDictionary in a separate assembly
提问by Gus Cavalcanti
I have resource dictionary files (MenuTemplate.xaml, ButtonTemplate.xaml, etc) that I want to use in multiple separate applications. I could add them to the applications' assemblies, but it's better if I compile these resources in one single assembly and have my applications reference it, right?
我有我想在多个单独的应用程序中使用的资源字典文件(MenuTemplate.xaml、ButtonTemplate.xaml 等)。我可以将它们添加到应用程序的程序集中,但是如果我在一个程序集中编译这些资源并让我的应用程序引用它会更好,对吗?
After the resource assembly is built, how can I reference it in the App.xaml of my applications? Currently I use ResourceDictionary.MergedDictionaries to merge the individual dictionary files. If I have them in an assembly, how can I reference them in xaml?
资源程序集构建完成后,如何在我的应用程序的 App.xaml 中引用它?目前我使用 ResourceDictionary.MergedDictionaries 来合并各个字典文件。如果我在程序集中有它们,我如何在 xaml 中引用它们?
回答by Kent Boogaart
Check out the pack URI syntax. You want something like this:
查看包 URI 语法。你想要这样的东西:
<ResourceDictionary Source="pack://application:,,,/YourAssembly;component/Subfolder/YourResourceFile.xaml"/>
回答by Hertzel Guinness
An example, just to make this a 15 seconds answer -
一个例子,只是为了让这个回答 15 秒 -
Say you have "styles.xaml" in a WPF library named "common" and you want to use it from your main application project:
假设您在名为“common”的 WPF 库中有“styles.xaml”,并且您想在主应用程序项目中使用它:
- Add a reference from the main project to "common" project
- Your app.xaml should contain:
- 添加从主项目到“公共”项目的引用
- 您的 app.xaml 应包含:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Common;component/styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Cheers
干杯
回答by Aleksandar Toplek
I'm working with .NET 4.5 and couldn't get this working... I was using WPF Custom Control Library. This worked for me in the end...
我正在使用 .NET 4.5 并且无法让它工作......我使用的是 WPF 自定义控件库。这最终对我有用......
<ResourceDictionary Source="/MyAssembly;component/mytheme.xaml" />
source:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/11a42336-8d87-4656-91a3-275413d3cc19
来源:http : //social.msdn.microsoft.com/Forums/en-US/wpf/thread/11a42336-8d87-4656-91a3-275413d3cc19
回答by CharithJ
Resource-Only DLL is an option for you. But it is not required necessarily unless you want to modify resources without recompiling applications. Have just one common ResourceDictionary file is also an option. It depends how often you change resources and etc.
仅资源 DLL 是您的一个选择。但不是必须的,除非您想在不重新编译应用程序的情况下修改资源。只有一个通用的 ResourceDictionary 文件也是一种选择。这取决于您更改资源的频率等。
<ResourceDictionary Source="pack://application:,,,/
<MyAssembly>;component/<FolderStructureInAssembly>/<ResourceFile.xaml>"/>
MyAssembly- Just assembly name without extension
MyAssembly- 只是没有扩展名的程序集名称
FolderStructureInAssembly- If your resources are in a folde, specify folder structure
FolderStructureInAssembly- 如果您的资源在一个文件夹中,请指定文件夹结构
When you are doing this it's better to aware of siteOfOriginas well.
当您这样做时,最好也了解siteOfOrigin。
WPF supports two authorities: application:/// and siteoforigin:///. The application:/// authority identifies application data files that are known at compile time, including resource and content files. The siteoforigin:/// authority identifies site of origin files. The scope of each authority is shown in the following figure.
WPF 支持两种权限:application:/// 和 siteoforigin:///。application:/// 权限标识在编译时已知的应用程序数据文件,包括资源和内容文件。siteoforigin:/// 权限标识源文件的站点。每个权限的范围如下图所示。


回答by Gianluca Demarinis
For UWP:
对于 UWP:
<ResourceDictionary Source="ms-appx:///##Namespace.External.Assembly##/##FOLDER##/##FILE##.xaml" />
回答by Kylo Ren
Using XAML:
使用 XAML:
If you know the other assemblystructure and want the resourcesin c#code, then use below code:
如果你知道其他的assembly结构,并希望resources在C#代码,然后用下面的代码:
ResourceDictionary dictionary = new ResourceDictionary();
dictionary.Source = new Uri("pack://application:,,,/WpfControlLibrary1;Component/RD1.xaml", UriKind.Absolute);
foreach (var item in dictionary.Values)
{
//operations
}
Output:If we want to use ResourceDictionaryRD1.xamlof Project WpfControlLibrary1into StackOverflowAppproject.
输出:如果我们想将ResourceDictionaryRD1.xamlProject使用WpfControlLibrary1到StackOverflowApp项目中。
Structure of Projects:
项目结构:
Code Output:
代码输出:
PS:All ResourceDictionaryFiles should have Build Actionas 'Resource' or 'Page'.
PS:所有ResourceDictionary文件都应Build Action为“ Resource”或“ Page”。
Using C#:
使用 C#:
If anyone wants the solution in purely c# codethen see my this solution.
如果有人想要纯c# 代码中的解决方案,请参阅我的此解决方案。

