I find that having just one XAML
ResourceDictionary file with all of your globally available
StaticResources defined in it can easily expand to enormous sizes and get very messy. This makes every developer's life unnecessarily harder, as a search for a very particular information (e.g. a
Color) might turn into a proverbial finding of a needle in a haystack. It also often leads to creating duplicates of already existing resources - as the search of the needle haven't been successful.
From my experience, the best way of avoiding this kind of situations is dividing applications resources into multiple categories and creating separate resource files for each. I usually split these into the following categories:
- Styles and Templates
I feel like most of the above categories are self explanatory. I might mention though that in the Dimensions resource dictionary I would define all of the
Padding and other spacing values, it would also include all of the font sizes used across the app,
Obviously, this is just the basics, you could go much further with the break down of these, to suite your needs better. For instance, instead of having all of the
Styles defined in one place you could organise them by the control type (e.g. ButtonStyles).
To combine all of these
ResourceDictonary files in XAML you will need to take advantage of the ability to merge dictionaries. Assuming that all of these resources will be globally accessible you should update your
App.xaml file to include these in:
<?xml version="1.0" encoding="utf-8" ?> <Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:resources="clr-namespace:YourAppName.UI.Resources;assembly=YourAppName.UI" x:Class="YourAppName.UI.App"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <resources:DimensionsResources /> <resources:ColorsResources /> <resources:ImagesResources /> <resources:ConvertersResources /> <resources:StylesAndTemplatesResources /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>
Note in the above code usage of the
ResourceDictionary.MergedDictionaries, which merges all of the defined resources in separate
ResourceDictionary files into one.
Be also aware that some of the defined styles and templates might rely on the Colors or Dimensions defined in their respective files, therefore these will need to be merged in before Styles and Templates.