SirJohnK/LocalizationResourceManager.Maui

At least one resource must be added with Settings.AddResource!

Closed this issue · 2 comments

I'm currently migrating my Xamarin.Forms app to Maui. Upon initializing my first View I receive the following error:

System.InvalidOperationException: At least one resource must be added with Settings.AddResource!

I'm adding the resource as described in my MauiProgram.cs:

.UseLocalizationResourceManager(settings =>
{
    settings.AddResource(AppLang.ResourceManager);
    settings.RestoreLatestCulture(true);
});

What am I missing? Using v1.2.1

Hello, @Flying--Dutchman!

I can only think of 2 scenarios where this can happen.

  1. If there is an issue with the ResourceManager file/setup when adding.
  2. The GetValue function is called before any resources are added.

That exception is only thrown here:

public string GetValue(string text)
{
    //Verify Resources
    if ((resources?.Count ?? 0) == 0)
        throw new InvalidOperationException($"At least one resource must be added with Settings.{nameof(AddResource)}!");

Verify AddResource by looking at the return status while debugging:

.UseLocalizationResourceManager(settings =>
{
    var status = settings.AddResource(AppLang.ResourceManager);

If this returns False, then there is an issue with the resource file.
I would recreate the Resource/resx file with the VS Template and copy over the resources/texts from the original file:

image

Hope this helps!

/Regards Johan

The return value was indeed false. Apparently the *.Designer.cs was not being recreated, and so it had the wrong namespace. Manually updating the namespace resolved the issue.

Thank you very much!