How do I access a RESX file on Xamarin.iOS?

EasyGoingPatEasyGoingPat GBMember ✭✭✭


I have created a new .resx file in the \Resources folder of my Xamarin.iOS application, but I can't figure out how to read the values in it.

The method used inside the PCL for the solution - Application.Current.Resources[ key ] - doesn't work, and the only example the docs seem to cover is localisation, which is a bit different.

I having a 'thick Friday', so any help would be appreciated.

Kind wishes ~ Patrick


Best Answer


  • EasyGoingPatEasyGoingPat GBMember ✭✭✭


    According to the documentation here, this class appears to be what I need: System.Resources.ResXResourceReader. But it doesn't seem to be available.

  • EasyGoingPatEasyGoingPat GBMember ✭✭✭

    Thank you for the reply, @ashalva. I'm not clear exactly what you mean. Should I be using ResXResourceReader or reading the file using a different method.

    I'm sorry - I don't think my original question was clear. It is the access to the class ResXResourceReader that I don't appear to be able to get. I haven't got as far as worrying about the actual file path and name yet.

    Incidentally, what is the correct build option for a .resx file? I though it was BundleResource.

    • Patrick
  • EasyGoingPatEasyGoingPat GBMember ✭✭✭

    It seems that a .resource file is a .resx file that has been compiled into binary. To compile it, you use resgen and then the resulting binary has to be bundled with your application. I think you then need the ResXResourceReader class to access the compiled binary. Xamarin does not provide this (it is in the System.Resources namespace, in assembly System.Windows.Forms). However, if you just want to use a .resx file to store a few configuration values, all of this might seem like overkill.

    An alternative is to simply to use .resx files as text-based resources (actually they are XML). This allows you to use Visual Studio's neat editor to add name-value pairs (and give them an optional comment). The .resx files stay in the normal resource location for your project (on iOS, this is the \Resources folder), and no special build action is required, because files placed here are automatically made available to your running application.

    Doing this will incur a small size overhead (because XML is bigger than binary) and there is a small runtime cost (because the XML has to be parsed), but these are negligible as long as your files are not too big.

    I've written a small class, which neatly wraps up the reading of these raw XML .resx files. It works a treat, with none of the hassle of trying to incorporate resgen into my build process.

    I have not had the time to turn it into a proper online article, but I have attached the code, if you would like to see how to do this.

    Kind wishes ~ Patrick

    PS - If you find this useful, it might be worth marking this as an answer, so others can find it and use it.

Sign In or Register to comment.