我正在开发 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:
截图:
这里出了什么问题?请有人帮我解决这个问题。
答案 0 :(得分:0)
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;