Resource Dictionary is a foundational building block to all Xamarin.Forms mobile development. It’s the primary vehicle for declaring XAML resources you’ll use throughout your mobile application, including everything from styles to value converters, content templates, and data templates. In the most recent stable release of Xamarin.Forms, we gained the ability to merge multiple resource dictionaries for even greater flexibility in composing resources. In this article, I’ll show you how to take advantage of that and give you a glimpse at some of the new syntax improvements coming your way.

Merged Dictionaries

Out with the Old

Prior to 2.5.0, you probably found yourself implementing a single resource dictionary per ContentPage or other control. Every control has a Resources property of type ResourceDictionary where you declare reusable resources.

You might define colors and styles that you wish to use throughout your application at the Application level in this code.

If you’re really industrious, you might have discovered that you merge one dictionary with another using the MergedWith property.

In this example the primary dictionary is being merged with another resource dictionary of colors.

Using a ResourceDictionary in this fashion requires a code file to declare the class.

In with the New

This syntax gets much cleaner with the new implementation of merged dictionaries, and you can merge multiple dictionaries instead of just the one. Instead of using MergedWith, you now add to a new MergedDictionaries collection property.

A big thanks to Microsoft MVP Adam Pedley for his proposal and initial pull request for merged dictionaries and for working with us to make this happen.

Upcoming ResourceDictionary Improvements

The next major version of Xamarin.Forms will introduce several improvements to ResourceDictionary in XAML, which you can get a taste for now in the nightly build feed.

Pure XAML ResourceDictionaries
You’ll notice the Colors.xaml needs to have a partial class to accompany it in the example above, even though it’s totally empty. That’s no longer required in the nightly build, where you can declare a XAML file without any Class and it will still be compiled with XAML.

Here’s another file Colors.xaml example, which contains all the colors to be used in an application. The <?xaml-comp compile=”true” ?>  does the magic.

More Concise Syntax
XAML can get pretty verbose as you declare all the resources for your application, so we’ve done some work to cut the corners and save some keystrokes. Instead of writing:

You can simply write:

In this new syntax:

  • Source: You can now load your dictionary with the new Source property. The file should be relative to the document or absolute from the application root
  • No need to specify a top level <ResourceDictionary>, as it’s now created on demand
  • No need to specify <ResourceDictionary.MergedDictionaries> if your StyleSheet or ResourceDictionary is keyless

These improvements provide more flexibility for you to organize your application resources and compose them on a control however you need them.

Be Resourceful

Multiple merged dictionaries are available today on NuGet in the Xamarin.Forms 2.5.0 stable release. Be sure to also check out the nightly feed and explore the new features we have on the horizon.

Do you have an idea or request like Adam’s that you’d like to contribute? Start by exploring the enhancements project board on GitHub, and then open an enhancement proposal.