C# 是否可以在多个项目之间共享 ResourceDictionary 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/946869/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:59:38  来源:igfitidea点击:

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

c#wpfxamlresourcedictionary

提问by Mark Carpenter

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.

如果我在一个项目中有一个 ResourceDictionary,是否可以创建另一个使用第一个项目中定义的资源的项目?请注意,这两个项目都是 WPF 应用程序,而不是 ControlLibraries。

Thanks!!

谢谢!!

采纳答案by user112889

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

是的,当然这是可能的,只要项目 B 有对项目 A 的引用。

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

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

然后您可以只使用在 YourResourceFile.xaml 中定义的资源。

回答by David Dowdle

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.

我发现我必须引用程序集本身而不是使用项目名称。我也不需要使用 pack:/// 语法来让它工作。

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

这个重复问题的答案指定了要使用的格式(我可以验证此语法在 .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:

因此,如果您在根级别有一个程序集 Abc.Def.dll 和该 DLL 中名为 Xyz.xaml 的文件,您将使用:

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

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

注意:Resharper 7 指出我必须引用程序集本身。