从外部文件或程序集加载 WPF 样式或其他静态资源

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

Load WPF styles or other Static Resources from an external file or assembly

wpfxamlassembliesstylesresourcedictionary

提问by Shimmy Weitzhandler

I have a few WPF applications and I want all my styles to be in a shared assembly instead of declaring them in each application separately.

我有几个 WPF 应用程序,我希望我的所有样式都在一个共享程序集中,而不是在每个应用程序中单独声明它们。

I am looking for a way so I don't have to change all my Style="{StaticResource BlahBlah}"in the existing applications; I just want to add the reference to this style assembly, and delete it from the current application, so it's taken from the assembly.

我正在寻找一种方法,这样我就不必更改Style="{StaticResource BlahBlah}"现有应用程序中的所有内容;我只想添加对此样式程序集的引用,并将其从当前应用程序中删除,因此它是从程序集中获取的。

Is there any way?

有什么办法吗?

回答by Shimmy Weitzhandler

Referencing an external ResourceDictionary (XAML File):

引用外部 ResourceDictionary(XAML 文件):

<Application.Resources>
    <ResourceDictionary Source="MyResources.xaml" />
</Application.Resources>

Referencing an external ResourceDictionary (DLL):

引用外部 ResourceDictionary (DLL):

<Application.Resources>
    <ResourceDictionary Source="/MyExternalAssembly;component/MyResources.xaml" />
</Application.Resources>