Is it possible to use a portable library with localized resources in a Universal app?

喜欢而已 提交于 2019-12-02 23:44:36

You can have .resx files in pcls, you just have to use the workaround explained on http://blogs.msdn.com/b/philliphoff/archive/2014/11/19/missingmanifestresourceexception-when-using-portable-class-libraries-in-winrt.aspx

Even with the latest VS 2013 (update 5) the mdil error will appear when trying to deploy Release packages.

MdilXapCompile.exe failed with error code 1004

To work around it you must compile your app using the store option and then use the Application Deployment tool to actually deploy it to the device.

Unfortunately the workaround described on Phil Hoff blog did not work for me too well. I have developed my own workaround. It turns out if you are using .resx files to store string values only, then you can easily convert them to .resw and use natively in Windows Phone 8.1.

So what I am doing is copying and converting resources from PCL and placing as native .resw resources in Windows Phone project every build automatically using the tool I made - ResxHell. Just follow the instructions on the repository and you can then reach resources like this

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

For nice binding I ended up creating ValueConventer and small localization helper class, take a look at this gist: Binding from .resw files example

With the use of that you can do following in your xaml pages:

//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}}"/>

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

You certainly unchecked the "Build" checkbox in the Configuration Manager, that's why the resources are not copied automatically. Check it and it will works.

In the end, it seems you can't use PCL library with localized resources using .resx files in the WP part of a Universal app. At least it's not trivial.

There were runtime problems when the app is build in Realese configuration, which I couldn't resolve, so I decided to change to .resw resources. This solved my problems, of course, but I had to duplicate the resources, which is what I was trying to avoid.

Had the same problem. I tried deploying via Windows Phone Deployment Tool and it worked. So instead of using Visual Studio to deploy: create packages and put them on phone and it works!!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!