Corrado's Blog 2.0

Online thoughts of a technology funatic

Application wide resources (reloaded)

Back in September 2014 I blogged about adding application wide resources to Xamarin Forms applications, that technique is no longer needed since starting from v 1.3 Xamarin added support to app wide resources into Xamarin Forms core.

So what it this post about? It is about adding (and using) them in a more user-friendly way.

Since v 1.3 you can create a resource that can be reachable from all pages this way:

public class App : Application
    {
        public App()
        {
            this.Resources = new ResourceDictionary
            {
                {"TextColor", Color.FromHex("00FF00")}
            };

            // The root page of your application
            this.MainPage = new MainView();
        }
    }

And use the resource from a XAML page this way:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App1.MainView">
    <Label Text="Hello from Xamarin Forms" 
             VerticalOptions="Center" 
             HorizontalOptions="Center" 
             TextColor="{StaticResource TextColor}" />
</ContentPage>

Ok, It works but is not very friendly, resource name is not listed in XAML Intellisense (powered by Resharper of course) and as XAML lover I’d like to define my application resources in XAML the same way I do in a page.

Luckily you can and it’s very easy:

1- Delete App.cs file

2-Add a new Forms XAML Page and name it App.cs

image

3-Make App.cs inherit from Application instead of ContentPage

public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
        }
    }

4-Replace the generated XAML with following one, don’t forget to substitute [YourNamespaceHere] with your own’s application namespace

<Application xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="[YourNamespaceHere].App">
    <Application.Resources>
        <ResourceDictionary>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Your project should now look like this:

image

and you can add your resources inside ResourceDictionary as in following example:

<Application xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="[YourNamespaceHere].App">
    <Application.Resources>
        <ResourceDictionary>
            <Color x:Key="TextColor">Red</Color>
        </ResourceDictionary>
    </Application.Resources>
</Application>

And now, your resource will work as expected but it will also be listed by Intellisense

image

Definitively more friendly than using code (IMHO)

Hope to see this feature added to Xamarin Forms template soon.

1 Comment “Application wide resources (reloaded)”

  1. [...] Application wide resources (reloaded) (Corrado Cavalli) [...]

    Pingback by Dew Drop – March 23, 2015 (#1979) | Morning Dew — 23/03/2015 @ 13:02

RSS feed for comments on this post. TrackBack URL

Leave a Response