Xamarin.Forms + .resx string resources - System.IO.FileNotFoundException: Invalid Image
Asked Answered
A

1

8

I am creating a Xamarin.Forms app based on a C# shared code project and another two projects for the actual Android + iOS app.

I now want to implement string localization like it is documented here. So I created a .net standard 2.0 library project in my solution for the .resx file and referenced this project in my two main projects, and I did all the other steps described in the linked article, of course.

When I now want to access any of my string resources from one of my app projects, ResourceManager.GetString throws this Exception: System.IO.FileNotFoundException: Invalid Image

Example code would be that line, but it can be also any other string resource.

public static string global_copyright {
    get {
        return ResourceManager.GetString("global_copyright", resourceCulture);
    }
}

I can confirm that the assembly of this library project is found and loaded correctly, since I can create instances of other classes defined in that project.

Now I put this code directly at the beginning of the App() constructor like it is described in that article to debug for such issues:

var assembly = typeof(AppResources).GetTypeInfo().Assembly;
foreach (var res in assembly.GetManifestResourceNames())
{
    System.Diagnostics.Debug.WriteLine("found resource: " + res);
}

var test = AppResources.global_copyright;

The first lines are executed fine and it shows me that one resource file is found in that assembly. Its name is also correct. But on the last line it crashes with that exception. What did I wrong? Why is ResourceManager not able to load string resources from that assembly?

Visual Studio 2017 (15.9.6) with Xamarin.Forms v3.4

Abidjan answered 31/1, 2019 at 17:0 Comment(5)
maybe you could place all the resource strings into the same resx file and then had all 3 resourcemanagers reference the same file.Alligator
Did you manage to fix this issue? I have identical problem albeit still using portable.Monogamist
I just posted a possible workaround for that issue as an answer below.Abidjan
why not just use ResX manager VS extension and avoid the hassleTegucigalpa
@NickKovalsky because ResX Manager just provides you a more convenient UI to edit your resx resource files. I'm using it by the way. But the generated C# code to access your string resources remains the same as if you would just use the default resource editor of VS. So also the exceptions are the same.Abidjan
A
5

Since there is no real answer yet, I want to present a workaround as a preliminarily answer until someone find something better.

First, the Problem still exists in Visual Studio 2019 (16.1.2) with Xamarin.Forms v3.6

I found out that it occurs because in Visual Studio Exception Settings I checked to break on all "Common Language Runtime Exceptions". When I disable that Setting, the app can be debugged without any problems and the resource strings are loaded correctly.

Next, I found out that it seems to be an internal exception of the ResourceManager that is for whatever reason bubbled up into my own code. Simply press F5 to continue debugging when this exception is raised (has to be done twice, but only the first time you access your resources) and the app continues running normally.

So there are three possible workarounds:

  1. Disable to break on "Common Language Runtime Exceptions" in Exception settings Screenshot
  2. In Exception Settings, let "Common Language Runtime Exceptions" checked but disable just the "System.IO.FileNotFoundException" break. Screenshot 2
  3. Just continue Debugging with F5 when that exception is thrown
Abidjan answered 12/6, 2019 at 6:37 Comment(3)
ok thats a "workaround", but no solution. You never get filenotfound exception when they are appropriate... hmQuinidine
100% agree - but until now I couldn't find any better solutions. I still wanted to share this because I spend a lot of time to find out what's wrong when I started working with resource files - until I realized there is nothing wrong with my code but it's just an Exception that can be ignored and that shouldn't be catched by the Debugger.Abidjan
Hi guys, any new about this issue? I have the same issue, VS 2019 16.3.9 xamarin.forms 4.2.0.848062Christlike

© 2022 - 2024 — McMap. All rights reserved.