简体   繁体   中英

Unable to access .resx file from Windows 8.1 Runtime windows store app project

I am developing Windows Phone 8.1(WinRT) windows store app project. I have developed different PCL project for DataConnection, ServiceConnection etc...which I am referencing in WP 8.1 project. In DataConnection pcl I have all .resx files which were created under .net 4.0 target. All my PCL projects were created in Xamarin Studio for cross platform(Android, iOS, WP). Now I opened my project in Visual Studio Premium 2013 in-order to develop WP project and changed my target framework from .net 4.0 to 4.5, since WP 8.1 requires 4.5 as target.

After changing, I am unable to read string resources values from Windows Phone project. While executing below line,

string test = Test.String1;

It throws exception as,

An exception of type 'System.ArgumentNullException' occurred in mscorlib.ni.dll but was not handled in user code

Additional information: Value cannot be null.

Exception Trace:

System.ArgumentNullException was unhandled by user code
  HResult=-2147467261
  Message=Value cannot be null.
Parameter name: format
  Source=mscorlib
  ParamName=format
  StackTrace:
       at System.String.Format(IFormatProvider provider, String format, Object[] args)
       at System.Environment.GetResourceString(String key, Object[] values)
       at System.Resources.ResourceManager.GetString(String name, CultureInfo culture)
       at App.Core.Resources.Test.get_String1()
       at App.Forms.WindowsPhoneUI.MainPage..ctor()
       at App.Forms.WindowsPhoneUI.App_Forms_WindowsPhoneUI_XamlTypeInfo.XamlTypeInfoProvider.Activate_4_MainPage()
       at App.Forms.WindowsPhoneUI.App_Forms_WindowsPhoneUI_XamlTypeInfo.XamlUserType.ActivateInstance()
  InnerException: 

Screenshot:

在此处输入图片说明

What is going wrong here? Please someone help me to fix this.

This blog helped me to fix this issue. Although the exceptions were different, in the blog its mentioned as MissingManifestResourceException whereas I was getting ArgumentNull exception, but internal it was crashing in Getstring method in both the cases.

Code Referred from above blog:

I added below class in my project.

public class WindowsRuntimeResourceManager : ResourceManager
{

  private readonly ResourceLoader resourceLoader;

  private WindowsRuntimeResourceManager(string baseName, Assembly assembly) : base(baseName, assembly)
  {
    this.resourceLoader = ResourceLoader.GetForViewIndependentUse(baseName);
  }

  public static void InjectIntoResxGeneratedApplicationResourcesClass(Type resxGeneratedApplicationResourcesClass)
  {
    resxGeneratedApplicationResourcesClass.GetRuntimeFields()
      .First(m => m.Name == "resourceMan")
      .SetValue(null, new WindowsRuntimeResourceManager(resxGeneratedApplicationResourcesClass.FullName, resxGeneratedApplicationResourcesClass.GetTypeInfo().Assembly));
  }



  public override string GetString(string name, CultureInfo culture)
  {
    return this.resourceLoader.GetString(name);
  }
}

And in Windows Phone project before calling resource file, just have to call below method.

WindowsRuntimeResourceManager.InjectIntoResxGeneratedApplicationResourcesClass(typeof(App.Resources.Account));

            string test = App.Resources.Account.Error_CouldnotCreateUserProfile; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM