Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 521
Torben Schramme Avatar asked Jan 31 '19 17:01

Torben Schramme


1 Answers

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
like image 137
Torben Schramme Avatar answered Oct 23 '22 02:10

Torben Schramme