Dynamically binding RESX Resources in Xamarin Forms

By | May 20, 2017

Localizing your app isn’t the most fun thing to do. In the last couple of years I’ve seen and tried various ways of implementing multi language support.

In this blogpost I would like to share how I recently set up localization in a Xamarin Froms app. With this approach, I am able to bind to items in a RESX Resource file dynamically. What do I mean with dynamically? Well, as soon as the user changes his preferred language in the app, the value of all bound resource items are instantly updated to the value of the chosen langue.

If you don’t know what RESX resource files are, I recommend you quickly go through this: Localizing Xamarin.Forms Apps with RESX Resource Files.

Let’s take a look at dynamically binding RESX Resources

It is very simple! Not only the implementation itself, but also the usage of it.
I’ve created a class LocalizedResources  that looks like this:

The first thing to point out, is the ResourceManager field. This class can be used to look up culture-specific resources. In other words, if I have all my culture specific resources in place, I can use this class to retrieve a string for a particular culture. Let’s say I have some resource files and I want to get the French value of a particular key, I could do the following:

The resource that I pass into to constructor of the ResourceManager is a Type. It is the type from which the resource manager derives all information for finding the correct resource. More information can be found on Micrososft Docs.

Back to my LocalizedResources class. Now that we know what the ResourceManager can do, it should be clear in the above code that we use the  CurrentCultureInfo field to retrieve culture-specific resources from my RESX Resource files. We do this in the class’ indexed property:

This is the glue between my Resources and my UI! In Xamarin forms we can bind to indexed properties. Whatever value we put between the square brackets in our Binding statement in XAML is passed as key in our indexed property.

One more thing: in my LocalizedResources class, I subscribe to a Message CultureChangedMessage.  When this message is sent, I will set the CurrentCultureInfo field and trigger a PropertyChanged event, passing “item” as PropertyName (see my previous blogpost). All bindings on the index property are re-executed, resulting in getting the resource values of the newly set CultureInfo.

The only thing left to do is adding an instance of the LocalizedResources class to a ViewModel.

So at a high level, it all comes down to this:

Binding Resources overview







This sample app has two views: Main and Settings. On the Settings view, users are able to change their language. The labels in the app will be updated instantly.


(MainPageViewModel only inherits ViewModelPage, there is nothing special to show there)


Full source-code can be found on my GitHub.

Remarks: of course this is a bit of a naïve implementation as I just keep a CurrentLanguage field on the App class. This demo is just to show you how easy it is to implement localization of your app this way. If you want to do a ‘full’ implementation of localization, I would like to refer back to this guide on the Xamarin website and take a look at the ILocalize  interface and the implementations of it. Using that abstraction works great together with my solution of localization.

If you have any remarks or questions, please feel free to drop a comment below!

4 thoughts on “Dynamically binding RESX Resources in Xamarin Forms

  1. Pingback: Dew Drop - May 22, 2017 (#2484) - Morning Dew

  2. Pingback: The week in .NET – Open XML SDK, Adventure Time | .NET Blog

  3. Cristhian Amaya

    I was looking on how to do something like this, thanks for this post, it was really helpful!!

    I’ve got one question though, why do you create the CultureChangedMessage? You could do something like:

    MessagingCenter.Send(this, “CultureChanged”, new CultureInfo(“en”));
    MessagingCenter.Subscribe(this, “CultureChanged”, OnCultureChanged);


    1. PieEatingNinjas Post author

      you are absolutely right, the approach you suggests works as well. However, I like to use dedicated classes for my messages, because strings are a lot more error prone as you could easily mistype the name of your message. Of course you can fix this by referencing to strings in a static messages class or something like that.
      Both ways work perfectly. I think it is a matter of opinion and I suggest you use the approach you feel most comfortable with. 🙂


Leave a Reply