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

标签 c# localization windows-runtime windows-phone-8.1 portable-class-library

我无法访问面向 .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

重现:

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

在模拟器上运行良好,在部署到真实设备时不起作用

最佳答案

不幸的是,解决方法描述了 on Phil Hoff blog对我来说不太好。 我已经开发了自己的解决方法。事实证明,如果您使用 .resx 文件仅存储字符串值,那么您可以轻松地将它们转换为 .resw。

所以我正在做的是自动从 PCL 转换所有 .resx 文件并将其放入我的 Windows Phone 8.1 项目中的 native 结构化文件夹中,并使用我编写的这个工具在每次构建时刷新它们。

https://github.com/konradbartecki/ResxHell

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

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

为了实现良好的绑定(bind),我最终创建了 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}}"/>

或者 string localizedtext = LocalizationHelper.GetString("MyCustomReswFile", "MyStringId");

关于c# - 无法从 Windows Phone 8.1 访问 .NET 4.5 PCL 中的本地化资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33262347/

相关文章:

android - 更改android中的默认字符串xml文件

javascript - 如何从WinJS/javascript使用WebView控件navigateToLocalStreamUri

binding - 由于 UI 中的编辑导致目标属性更改后,依赖属性停止更新

c# - WinRT 上的 C++、C# 和 JavaScript

iphone - 有没有用于翻译 Localized.strings 文件的应用程序?

c# - Dapper Contrib 插入 MatchNamesWithUnderscores 映射器无法正常工作

c# - 如何在 CustomEditor 中选择嵌套的 ReorderableList 中的元素?

C# 窗体 : Set text in ComboBox based on selected Item

ios - 如何在不重新启动应用程序 swift4 的情况下更改本地化语言?

c# - 如何对 MySQL 查询进行排队,使它们顺序进行而不是并发,并防止过度使用 I/O?