无法从 Windows 8.1 运行时 windows 商店应用项目访问 .resx 文件
Unable to access .resx file from Windows 8.1 Runtime windows store app project
我正在开发 Windows Phone 8.1(WinRT) windows 商店应用程序 项目。我为 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:
截图:
这里出了什么问题?请有人帮我解决这个问题。
This 博客帮我解决了这个问题。尽管异常不同,但在博客中它被称为 MissingManifestResourceException,而我得到的是 ArgumentNull 异常,但在这两种情况下,它在内部都在 Getstring 方法中崩溃。
上面博客引用的代码:
我在我的项目中添加了下面 class。
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;
我正在开发 Windows Phone 8.1(WinRT) windows 商店应用程序 项目。我为 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:
截图:
这里出了什么问题?请有人帮我解决这个问题。
This 博客帮我解决了这个问题。尽管异常不同,但在博客中它被称为 MissingManifestResourceException,而我得到的是 ArgumentNull 异常,但在这两种情况下,它在内部都在 Getstring 方法中崩溃。
上面博客引用的代码:
我在我的项目中添加了下面 class。
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;