Sunday 16 September 2012

Windows Store Apps (XAML/C#) - Resource Files

It is important to keep the application structured and one of the key areas where this should happen is separating the application logic and content. Resource files is a great way to manage data for your application and even do it in context of your localization settings.

There a number of different ways to implement them, in this tutorial I will focus on three of them for Windows Store App development in XAML/C#:
  •  Manually retrieve values from a resource file from the code behind
  •  Bind a control property to a specific value in the resource file
  •  Have multilingual support by using resource files for each supported language


(A) Manually retrieve values from a resource file from the code behind:

In some cases we need retrieve and apply the content of the resource file entry from the code behind, so we do not want to tightly couple it with one element. This can be done as follows:
  • Open you solution and add a new resource folder to the project (Right click on project > Add > New Folder). In this case we are going to work with text so call it "Resources".
  • Add a resource file under the newly created "Resources" folder (Right click on folder > Add > New item > Resource File (.resw)
  •  Open the resource file and add a entry which value we can retrieve from the code behind.
  • Create a ResourceLoader instance in the code behind to connect to resource file (Do not specify a path or the extension of the resource file). Use the ResourceLoader instance to retrieve the value based on the key specified.


(B) Bind a control property to a specific value in the resource file

This creates a one to one one time binding from the resource file to a specific element property. You can do this by:

  • Create the element required to bind the value to in the XAML page. The important thing here is to use x:Uid as identifier NOT x:Name.   
 

  • Create both the "Resource" folder and resource file in a similar manner as mentioned in (A). However please note the changes in the Name section, this is used to identify the x:Uid and the property that should be changed.
 


(C) Have multilingual support by using resource files for each supported language

Resources files allow you to support more than one language and seamless integration to the localization settings of the user. For development it is important that you understand how multiple resource files are interpreted and ordered so that you can be sure that the correct resource file is selected from all the available ones (a more detailed example can be found here).
There are two main types of trees (ResourceMapSubTree) that resource files get grouped into:
  1. Files (This is for images, xaml and media files)
  2. Resources (This is for string resources)
Each of these will create a collection of available resources (Subtrees) that uniquely represent the available resource files (Same name) not the available context. For example the following will only create two Subtrees (custom and Resources):

 

Armed with this information, here is how you can enable multi-language support using resource files:
  • Create the a folder structure where the parent folders will depict the localization details for the added resource files (Be sure to use the correct language codes). Create the following structure:
  • Ensure that all the resource files have exactly the same elements (Name at least) with the corresponding translations of the Values column. Please take note that elements must be referenced by there x:Uid as we have done in (B) for this to seamlessly.

  •  If you want to control (Maybe for test purposes or language switch), you can manually define a preferred language that will then override your localization language (For startup do this in the App.xaml.cs file).








No comments:

Post a Comment