Merged ResourceDictionary vs App.xaml

◇◆丶佛笑我妖孽 提交于 2019-12-23 18:00:15

问题


I am reading up about the ResourceDictionary and have come to a confusing point.

It appears I can only have 1 ResourceDictionary per XAML. So, if I wanted to use more than one, I can Merge the ResourceDictionaries.

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.

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.


回答1:


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" />

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" />

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.

Hope this helps.



来源:https://stackoverflow.com/questions/15788869/merged-resourcedictionary-vs-app-xaml

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