合并的 ResourceDictionary 与 App.xaml
我正在阅读有关 ResourceDictionary 的内容,但遇到了一个令人困惑的问题.
I am reading up about the ResourceDictionary and have come to a confusing point.
看来每个 XAML 只能有 1 个 ResourceDictionary.所以,如果我想使用多个,我可以合并 ResourceDictionaries.
It appears I can only have 1 ResourceDictionary per XAML. So, if I wanted to use more than one, I can Merge the ResourceDictionaries.
如果我可以合并字典,那么全局"样式应该在哪里?我可以拥有一个 ApplicationResourceDictionary,其中包含在整个应用程序中保持一致的所有样式,或者,我可以将此信息保存到 App.xaml 文件中.两者似乎都是有效的选项,但我不知道是否是这种情况.
If I can merge dictionaries then where should 'global' styles live? I could have an ApplicationResourceDictionary with all the styles which are to be consistent throughout my application OR, I could save this information into the App.xaml file. both appear to be valid options but I don't know if that is the case.
这是关于个人选择还是一个比另一个更好?看起来将它们保留在 ResourceDictionaries 中更好,因为所有样式都在一起(在字典中),而不是在 XAML 页面中拆分一些.
Is this about personal choice or is one better than the other? It would appear keeping them in ResourceDictionaries is better because all styles are together (within the dictionaries) instead of splitting some in XAML pages.
我们当前的解决方案包含 100 多个项目.每个人都需要访问一些资源词典,其中包含主题和统一性等的全局资源.我为它做的是将资源词典集中在一个项目中,其他人引用,在这种情况下,我们称之为基础设施",然后我提供词典直接通过他们自己的 app.xaml 使用合并的字典直接到每个项目,例如;
Our current solution has 100+ projects in it. Each needing access to a few Resource Dictionaries with global resources for themes and uniformity etc. What I do for it is have the resource dictionaries centrally located in one project the others reference, in this case we call it "infrastructure" then I supply the dictionaries to each proj directly via their own app.xaml with merged dictionaries like for example;
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Our.Client.Infrastructure;component/Resources/Styles/ResDictName1.xaml" />
<ResourceDictionary Source="/Our.Client.Infrastructure;component/Resources/Styles/ResDictName2.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
到目前为止效果很好,我使样式应用全局的方式是在我们的自定义资源字典之一的底部指定它们,并从默认资源字典中删除相同的声明.例如,如果你发现;
Which so far works splendidly, the way I make the styles apply global though is specifying them as such at the bottom of one of our custom Resource Dictionaries and remove the same declaration from the Default Resource Dictionaries. So for example if you find;
<Style BasedOn="{StaticResource DefaultButtonStyle}" TargetType="Button" />
在您的默认 CoreStyles.xaml 或 SdkStyles.xaml 或任何它们可能是,我只是删除它,并将该声明移动到我们自定义资源字典的底部并相应地更改它;
in your Default CoreStyles.xaml or SdkStyles.xaml or whatever they may be, I just remove it, and move that declaration over to the bottom of our custom Resource Dictionary and change it accordingly like;
<Style BasedOn="{StaticResource OurSuperAwesomeCustomNewGlobalButtonStyle}" TargetType="Button" />
和瞧...任何 Button
之后默认继承我们的自定义样式而不是原始的默认模板.一旦你采用它,你的整个解决方案只拥有一两个资源词典的优势就会变得非常明显.也就是说,只要模板实际上需要全局可用.如果您将模板用于仅与其使用的视图相关的临时模板,请将其明确保存在该视图中,如果没有其他需要,则无需将其保存在其他地方.
and voila... Any Button
thereafter inherits our custom style by default instead of the original default template. The advantages of having just one or two Resource Dictionaries for your entire solution become clear real quick once you adopt it. That is, provided the template actually needs to be globally available. If you're using a template for something adhoc that only pertains to the view its used on, keep it in that view explicitly, no need to keep it somewhere else if nothing else needs it.
希望这会有所帮助.