Is it possible to share a ResourceDictionary file between multiple projects?

喜欢而已 提交于 2019-12-29 06:40:12

问题


If I have a ResourceDictionary in one project, is it possible to create another project that uses resources defined in the first project? Note that both projects are WPF Applications, not ControlLibraries.

Thanks!!


回答1:


Yes, of course that's possible, as long as Project B has a reference to Project A.

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/Project A;component/YourSubFolder/YourResourceFile.xaml" />
</ResourceDictionary.MergedDictionaries>

Then you can just use the Resources defined in YourResourceFile.xaml.




回答2:


I found that I had to reference the assembly itself and not use a project name. I also did not need to use the pack:/// syntax to get this to work.

This answer on the duplicate question specifies the format to use (I can verify that this syntax works in .NET 4.0): https://stackoverflow.com/a/10216253/1260563

Specifically (since I always forget the component part thinking it is a folder someone is using):

<ResourceDictionary.MergedDictionaries>
   <ResourceDictionary Source="/<YourAssemblyName>;component/<YourReferencedFileHere.xaml>" />
</ResourceDictionary.MergedDictionaries>

So if you have an assembly Abc.Def.dll and a file in that DLL called Xyz.xaml at the root level you would use:

<ResourceDictionary.MergedDictionaries>
   <ResourceDictionary Source="/Abc.Def;component/Xyz.xaml" />
</ResourceDictionary.MergedDictionaries>

Note: Resharper 7 pointed out that I had to reference the assembly itself.



来源:https://stackoverflow.com/questions/946869/is-it-possible-to-share-a-resourcedictionary-file-between-multiple-projects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!