Sharing Resource Dictionaries in your Xamarin.Forms App

The Xamarin.Forms Application base class has a Resources property that you can use to share a resource dictionary across all pages of your app. If your app was created before Xamarin.Forms 1.4 you'll need to make sure you update and make your main App class inherit from the new Application class.

If you like to do everything in code rather than XAML then you can create a ResourceDictionary in the constructor of you App class:

public class App : Application
{
    public App()
    {
        Resources = new ResourceDictionary();
        Resources.Add("ForegroundThemeColor",
            new Color(0.39f, 0.18f, 0.56f));
        // ..
    }
}

If you'd rather do this in XAML then you'll need to replace your App.cs class with an App.xaml class. This isn't part of the template at the moment, but this may change. So the easiest way to do it at the moment is to delete your App.cs class (making sure you save any extra stuff you've added). Then create a new XAML page and call it App.xaml.

Then go in and change the XAML route element from ContentPage to Application and add your resource dictionary like this:

<!--?xml version="1.0" encoding="UTF-8"?-->
<Application
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:class="NogginSample.App">
    <Application.Resources>
        <ResourceDictionary>
            <Color x:Key="ForegroundThemeColor">#632e8f</color>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Then copy any stuff from your old App class if you had one into your new App.xaml.cs file. Make sure you're calling InitializeComponent() in your App class constructor, or your XAML resource dictionary won't get built. Also if you're converting from a pre 1.4 version of Xamarin.Forms you need to move away from using the depricated static GetMainPage method and use the Application MainPage property instead.