Suggested modification to creation of CreateResourceLoader.

May 15, 2014 at 7:05 PM
I am porting a project from Windows Phone 8.0 to Windows Phone 8.1 RT (not Silverlight). I have found that the
built-in generator (PublicResXFileCodeGenerator) does not work. I found that ResWFileCodeGenerator does work except in design mode.
Following the desing practice for Windows Phone 8.0 I have the following class that allows me to reference the strings from xaml using binding:
public class LocalizedStrings
{
    private static Resources _localizedResources = new Resources();
    public Resources LocalizedResources { get { return _localizedResources; } }
}
I found that this works properly at runtime, however at design time the strings do not show and the design view shows an error indicating that the resource was not found.
If I modify the generated Resources.cs to call ResourceLoader.GetForCurrentView() instead of ResourceLoader.GetForCurrrentView("Resources") then it works properly in design view and I can see the default language strings at design time.

To remedy this, for my project, I have modified the creation of createResourceLoader in GenerateCode() in CodeDomCodeGenerator.cs to add a precondition that generates the following code inside the Resources constructor:
if(Windows.ApplicationModel.DesignMode.DesignModeEnabled)
{
    resourceLoader = ResourceLoader.GetForCurrentView();
}
else
{
    if(executingAssemblyName.Equals(currentAssemblyName))
    {
        resourceLoader = ResourceLoader.GetForCurrentView("Resources");
    }
    else
    {
        resourceLoader = ResourceLoader.GetForCurrentView(currentAssemblyName+"/Resources");
    }
}
This works exactly the same as the current code at runtime but at design time the no parameter varient of GetForCurrentView() is called. This also works if the depricated no parameter constructor new ResourceLoader() is called instead (tested) but opted to use the recommended call instead for VS2013.

I have not tested this solution outside of my environment so I do not know if it is a good general solution but
it does work for me and I would like something like it added to the official code base.

Here is what I did:

Modify code in CodeDomCodeGenerator.cs method GenerateCode():

Old Code:
        var createResourceLoader = new CodeConditionStatement(
            new CodeSnippetExpression("executingAssemblyName.Equals(currentAssemblyName)"),
            new CodeStatement[] // true
            {
                visualStudioVersion == VisualStudioVersion.VS2013
                    ? new CodeAssignStatement(new CodeFieldReferenceExpression(null, "resourceLoader"),
                                              new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("ResourceLoader"),
                                                                             "GetForCurrentView",
                                                                             new CodeSnippetExpression("\"" + className + "\"")))
                    : new CodeAssignStatement(new CodeFieldReferenceExpression(null, "resourceLoader"),
                                              new CodeObjectCreateExpression(new CodeTypeReference("ResourceLoader"),
                                                                             new CodeSnippetExpression("\"" + className + "\"")))
            },
            new CodeStatement[] // false
            {
                visualStudioVersion == VisualStudioVersion.VS2013
                    ? new CodeAssignStatement(new CodeFieldReferenceExpression(null, "resourceLoader"),
                                              new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("ResourceLoader"),
                                                                             "GetForCurrentView",
                                                                             new CodeSnippetExpression("currentAssemblyName + \"/" + className + "\"")))
                    : new CodeAssignStatement(new CodeFieldReferenceExpression(null, "resourceLoader"),
                                              new CodeObjectCreateExpression(new CodeTypeReference("ResourceLoader"),
                                                                             new CodeSnippetExpression("currentAssemblyName + \"/" + className + "\"")))
            });
New Code:
        var createResourceLoader = new CodeConditionStatement(
            new CodeSnippetExpression("Windows.ApplicationModel.DesignMode.DesignModeEnabled"),
            new CodeStatement[] // true
            {
                    visualStudioVersion == VisualStudioVersion.VS2013
                        ? new CodeAssignStatement(new CodeFieldReferenceExpression(null, "resourceLoader"),
                                                  new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("ResourceLoader"),"GetForCurrentView"))
                        : new CodeAssignStatement(new CodeFieldReferenceExpression(null, "resourceLoader"),
                                                  new CodeObjectCreateExpression(new CodeTypeReference("ResourceLoader")))
            },
            new CodeStatement[] // false
            {
                new CodeConditionStatement(
                new CodeSnippetExpression("executingAssemblyName.Equals(currentAssemblyName)"),
                new CodeStatement[] // true
                {
                    visualStudioVersion == VisualStudioVersion.VS2013
                        ? new CodeAssignStatement(new CodeFieldReferenceExpression(null, "resourceLoader"),
                                                  new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("ResourceLoader"),
                                                                                 "GetForCurrentView",
                                                                                 new CodeSnippetExpression("\"" + className + "\"")))
                        : new CodeAssignStatement(new CodeFieldReferenceExpression(null, "resourceLoader"),
                                                  new CodeObjectCreateExpression(new CodeTypeReference("ResourceLoader"),
                                                                                 new CodeSnippetExpression("\"" + className + "\"")))
                },
                new CodeStatement[] // false
                {
                    visualStudioVersion == VisualStudioVersion.VS2013
                        ? new CodeAssignStatement(new CodeFieldReferenceExpression(null, "resourceLoader"),
                                                  new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("ResourceLoader"),
                                                                                 "GetForCurrentView",
                                                                                 new CodeSnippetExpression("currentAssemblyName + \"/" + className + "\"")))
                        : new CodeAssignStatement(new CodeFieldReferenceExpression(null, "resourceLoader"),
                                                  new CodeObjectCreateExpression(new CodeTypeReference("ResourceLoader"),
                                                                                 new CodeSnippetExpression("currentAssemblyName + \"/" + className + "\"")))
                })
            });
Oct 13, 2014 at 5:53 PM
+1 Need this also!
Jan 29, 2015 at 4:52 PM
Personally this didn't work as well. Instead, I got working everything on an Universal App, replacing tha code piece with just:
resourceLoader = ResourceLoader.GetForCurrentView(currentAssemblyName+"/Resources");
I can provide a compiled version until the staff corrects the issue