系统相关
首页 > 系统相关> > c#-无法从Windows Phone 8.1访问.NET 4.5 PCL中的本地化资源

c#-无法从Windows Phone 8.1访问.NET 4.5 PCL中的本地化资源

作者:互联网

在以.NET 4.5为目标的可移植类库中访问本地化的字符串资源时遇到麻烦.

我允许用户在首页上选择语言,并在以后的页面上有本地化的经验.我试图通过获取代码资源来实现这一目标

MyTextBloxk.Text = PasswordResetMethod_Page.Title;

PasswordResetMethod_Page是.resx中自动生成的类

在WP 8.1模拟器上,一切正常,但是当我尝试将其部署到实际设备时,我得到了

Error : DEP6810 : MdilXapCompile.exe failed with error code 1004. See
log file
‘C:\Projects\WP81-ResourceBug\ResourceBugRepro.WP81\obj\Debug\MDIL\MDILXapCompileLog.txt’
for more details.

Error: Compile filter argument specified non-existent file:
C:\Projects\WP81-ResourceBug\ResourceBugRepro.WP81\obj\Debug\MSIL\ar\ResourceLib.resources.dll

Invalid argument

复制:

>克隆仓库https://github.com/konradbartecki/WP81-ResourceBug
>将WP8.1设置为启动项目
>部署到设备

在仿真器上工作正常,在部署到真实设备时不起作用

解决方法:

不幸的是,描述on Phil Hoff blog的变通办法对我来说效果不是很好.
我已经开发了自己的解决方法.原来,如果您使用.resx文件仅存储字符串值,则可以轻松地将它们转换为.resw.

因此,我要做的是自动将PCL中的所有.resx文件转换为Windows Phone 8.1项目中的本机结构化文件夹,并使用我编写的此工具刷新它们的每个版本.

https://github.com/konradbartecki/ResxHell

然后,我可以从这样的代码轻松访问我的字符串资源

var resourceLoader = new ResourceLoader();
var localizedText = resourceLoader.GetString("MyCustomReswFile/MyCustom");

为了实现良好的绑定,我最终创建了ValueConventer和小型本地化帮助器类,请查看以下要点:Binding from .resw files example

使用它,您可以在xaml页面中执行以下操作:

//For resource in file Page.Login.resw and string ID "NotUserYet"
<TextBlock Text="{Binding ConverterParameter=Page.Login/NotUserYet, Converter={StaticResource ResString}, Mode=OneWay, Source={StaticResource ResString}}"/>

要么
字符串localizedtext = LocalizationHelper.GetString(“ MyCustomReswFile”,“ MyStringId”);

标签:windows-runtime,windows-phone-8-1,portable-class-library,c,localization
来源: https://codeday.me/bug/20191119/2037037.html