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!

14 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. 🙂

  4. Raj Kumar Santhoshi

    I want to implement the Indian languages, I added the new resx files in your sample, however only Hindi is working no other language like Tamil or Gujrati or Marathi working

    1. PieEatingNinjas Post author

      Hi Raj. Interesting case. Could you please provide me your code? I’d love to take a look at it.

      1. payal

        HI, Is it possible to edit RESX files at runtime?
        Actually I will be getting the translated strings from webservice. so how can I edit resx files dynamically?

        1. PieEatingNinjas Post author

          Hi Payal. If your translations are dynamic, resx isn’t the right tool for the job.
          You can, however, use most of the code in my sample. But instead of going to look a specific string up in the resource file, look it up in your custom translation service which uses the (cached) data you retrieved from your backend service.

          Kind regards,

          1. payal

            Thanks alot 🙂
            It is possible for you to share sample app?

  5. Rajnikant

    This technique working fine in XAML, but what if i have a string in code behind.

    1. PieEatingNinjas Post author

      Hi! I don’t know exactly what you mean… Do you want to access your resources in code behind? Because that is really easy to do. For example (in code behind):
      string labelText = (this.BindingContext as MyViewModel).Resources["TheResourceYouWant"];
      Please let me know if this is the answer to your question, else you can give me some information about what you want to do.

  6. Sergey

    When you bind to viewmodel, which loads resources with some logic this method does not work 🙁 Do you know any elegant workaround?

  7. Дмитрий Грудкин

    This is a great solution, I’ve been looking for it for a very long time!
    There is one incomprehensible moment. I, like Rajnikant, want to use this solution in code, in a typical application with MasterDetailPage. I have a MasterPageItem class:

    public class MasterPageItem : BindableObject
    public static BindableProperty TitleProperty = BindableProperty.Create(
    propertyName: “Title”,
    returnType: typeof(string),
    declaringType: typeof(MasterPageItem),
    defaultValue: string.Empty,
    defaultBindingMode: BindingMode.OneWay,
    propertyChanged: TitlePropertyChanged);


    I create the ObservableRangeCollection () in the ViewModel:

    FirstPageItem = new MasterPageItem() { Icon = “learning.png”, TargetType = typeof(FirstPage) };
    FirstPageItem.SetBinding(MasterPageItem.TitleProperty, new Binding(path: “Resources[FirstPageMenuItemText]”, source: this));

    MainMenuItems = new ObservableRangeCollection()

    And I assign in the MainPage constructor (MainPage.xaml.cs):

    navigationDrawerList.ItemsSource = vModel.MainMenuItems;

    But it does not work. There are items on the menu (I add more than I gave here), but they do not contain text. How to fix it?

  8. Pingback: essayforme

Leave a Reply