String and Embedded Resources inside Windows Store App

String and Embedded Resources inside Windows Store App

It is pretty straight forward to access resources in inside Windows Store App, however there are few questions I came across on MSDN forum which I tried to answer. 
One of the interesting one was how to store resources inside RESW (Resource file) with name like SubTokenOne.SubTokenTwo.SubToken.  From first glance it looks simple but there is a problem. If we try to retrieve this using SubTokenOne.SubTokenTwo.SubToken, it will not be return the value.

By digging deep I found that, inside ResourceMap it is stored with key -  SubTokenOne/SubTokenTwo/SubToken instead of SubTokenOne.SubTokenTwo.SubToken, so to retrieve this we have to replace every . with / while using ResourceLoader.GetString("SubTokenOne/SubTokenTwo/SubToken")
Example 

var resourceLoader = new ResourceLoader(); //A.Name.Header //A/Name/Header var string = resourceLoader.GetString("A/Name/Header");



Another one was how to get all resource strings inside RESW. We can just iterate over all ResourceMap and query whether we need. 

	    string resourNameToFind = "String2"; // Replace with name to find
            ResourceMap namedResourceMap = Windows.ApplicationModel.Resources.Core.ResourceManager.Current.MainResourceMap;
            foreach (KeyValuePair<string, NamedResource> resNameToNamedResourcePair in namedResourceMap)
            {
                if (resNameToNamedResourcePair.Key.EndsWith(resourNameToFind)) // Either check or just iterate.
                {
                    IReadOnlyList<ResourceCandidate> lstResCandidates = resNameToNamedResourcePair.Value.ResolveAll();
                    foreach (ResourceCandidate resCandidate in lstResCandidates)
                    {
                        string value = resCandidate.ValueAsString; // Value of actual resource string in resw.
                    }
                }
            }

 
And then finally - How to access the file which build action as "embedded resource"? 

This while return the Stream object using identifier as shown below (We will need to use - using System.Reflection for this to work)-

var stream = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream("APP_NAME.FOLDER_NAME.Logo3.png");

var stream = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream("TestApp2.Assets.Logo3.png");

and to get name of all embedded resources try this -

string[] names = this.GetType().GetTypeInfo().Assembly.GetManifestResourceNames();


Leave a Comment
  • Please add 2 and 4 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Sachin  S edited Revision 3. Comment: Added using statement - using System.Reflection;

  • Sachin  S edited Revision 1. Comment: Updated title

Page 1 of 1 (2 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Sachin  S edited Revision 1. Comment: Updated title

  • Sachin  S edited Revision 3. Comment: Added using statement - using System.Reflection;

Page 1 of 1 (2 items)