简体   繁体   中英

ResourceDictionary organization in WPF style library

We have a WPF library that contains styles that are used in many of our products. It is organized as follows. Each indented level is merged into the library above

WpfResources.xaml
  Color.xaml
  ButtonStyle.xaml
    Color.xaml
  OtherStyle.xaml
    Color.xaml
  SliderStyle.xaml
    Color.xaml

The problem with this approach is that as you can see Color.xaml is added to each and every control style I add. This might and I'm not sure give a performance hit. All colors brushes, margins etc in Color.xaml are frozen though.

If I remove Color.xaml from the control ResourceDictionaries the visual studio designer will start complaining about missing StaticResources for colors and other things declared in Color.xaml. I could replace them by DynamicResources but that is not wise performance wise.

Any ideas on a better organization of styles?

In our application, we have a ResourceDictionary only for colors. So every color in our app is loaded in this files. Then we have our Styles and Templates XAML files for all controls, not separated ones like you put it.

One thing I've learned is, if you're repeating something, it could be better. Here's our App.xaml file just for an example. Keep in mind that with one Color.xaml you can make a lot of Themes/Skins!
When someone asks for aa new style, it only takes about 10 minutes to get it done! Here it is:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/Cmp.Wpf;component/CmpEstilos/Colors.xaml" /> <!-- Our Colors are here! -->
    <ResourceDictionary Source="pack://application:,,,/Cmp.Wpf;component/CmpEstilos/Styles.xaml" /> <!-- Our Styles are here! -->
    <ResourceDictionary Source="pack://application:,,,/Cmp.Wpf;component/CmpTemplates/Templates.xaml" /> <!-- Our Templates are here! -->
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

This code is inside App.xaml - just to remind you.
Nothing against your approach but I think that it would be easier to maintain if you have one file responsible for each "responsibility".

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM