【问题标题】:Unable to access .resx file from Windows 8.1 Runtime windows store app project无法从 Windows 8.1 运行时 Windows 商店应用程序项目访问 .resx 文件
【发布时间】:2015-06-25 11:18:05
【问题描述】:

我正在开发 Windows Phone 8.1(WinRT) windows store app 项目。我为 DataConnection、ServiceConnection 等开发了不同的 PCL 项目……我在 WP 8.1 项目中引用了这些项目。在 DataConnection pcl 中,我拥有在 .net 4.0 目标下创建的所有 .resx 文件。我所有的 PCL 项目都是在 Xamarin Studio 中为跨平台(Android、iOS、WP)创建的。现在我在 Visual Studio Premium 2013 中打开了我的项目,以便开发 WP 项目并将我的目标框架从 .net 4.0 更改为 4.5,因为 WP 8.1 需要 4.5 作为目标。

更改后,我无法从 Windows Phone 项目中读取字符串资源值。在执行以下行时,

string test = Test.String1;

它抛出异常,

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

Additional information: Value cannot be null.

异常跟踪:

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: 

截图:

这里出了什么问题?请有人帮我解决这个问题。

【问题讨论】:

  • 检查您的 Test 对象是否不为空。

标签: c# .net windows-store-apps resourcebundle


【解决方案1】:

This 博客帮助我解决了这个问题。尽管异常不同,但在博客中提到它为 MissingManifestResourceException 而我得到 ArgumentNull 异常,但在这两种情况下,它在内部都在 Getstring 方法中崩溃。

以上博客引用的代码:

我在我的项目中添加了下面的类。

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);
  }
}

在Windows Phone项目中调用资源文件之前,只需要调用下面的方法即可。

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

            string test = App.Resources.Account.Error_CouldnotCreateUserProfile; 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多