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 accross 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();
            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"?-->
			<Color x:Key="ForegroundThemeColor">#632e8f</color>

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.

1 Comment

  • Milton said

    Hi, i tried it, but when i tried to call InitializeComponent() its no posible because its method of ContentPage and not of Applicaction.

Comments have been disabled for this content.