mauiresourcedictionary

AppResources.ResourceManager crashes with FileNotFound-Exception


In an .NET MAUI App, I use AppResources.ResourceManager to translate resources from .resx-Files.

Already during Startup of my App in Pixel 5 - API 33 (Android 13.0 - API 33) Emulator I get a couple of FileNotFound-Exceptions:

MauiProgram.cs

var builder = MauiApp.CreateBuilder();
builder
    .UseMauiApp<App>()
    .UseMauiCommunityToolkit()
    .UseSharpnadoTabs(loggerEnable: false)
    .UseSharpnadoCollectionView(loggerEnable: false)
    .UseLocalizationResourceManager(settings =>
    {
        settings.RestoreLatestCulture(true);

        // FileNotFoundException here
        settings.AddResource(AppResources.ResourceManager);
    })
    ...

App.xaml

public partial class App : Application
{
    public App()
    {
        InitializeComponent();          // FileNotFoundException here
        ...
    }
}

LanguageResourceManager.cs

public class LanguageResourceManager : INotifyPropertyChanged
{
    public LanguageResourceManager()
    {
        AppResources.Culture = CultureInfo.CurrentCulture;
    }

    public static LanguageResourceManager Instance { get; } = new LanguageResourceManager();

    // FileNotFoundException here
    public object this[string resourceKey] => AppResources.ResourceManager.GetObject(resourceKey, AppResources.Culture) ?? Array.Empty<byte>();

    public event PropertyChangedEventHandler PropertyChanged;

    public void SetCulture(CultureInfo culture)
    {
        AppResources.Culture = culture;
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(null));
    }
}   

CalculatorViewModel.cs

public partial class CalculateMacrosViewModel : ViewModelBase
{
    private readonly LanguageResourceManager LanguageResourceManager;
    private static string AppTitle;


    public CalculateMacrosViewModel()
    {
        LanguageResourceManager = LanguageResourceManager.Instance;
        AppTitle = Translate("AppTitle");    // FileNotFoundException end here
    }

    private string Translate(string resourceString, object parameter = null)
    {
        if (parameter == null)
        {
            // FileNotFoundException here
            return $"{LanguageResourceManager[resourceString]}";
        }

        return string.Format(LanguageResourceManager[resourceString].ToString(), parameter);            
    }
}

Is there anything wrong in my implementation or why do I have so many FileNotFound-Exceptions during startup of my app? Normally, I would say the resx-Dictionaries should already be integrated into the App and be available. Later on, I can work with the LanguageResourceManager without any problems, only during startup I get these Exceptions. Any idea what I am doing wrong here?


Solution

  • This is a remaining issue on the package and the developer is trying to find the answer. You can follow it up for more information.

    In addition, someone mentioned that it looks like be a MAUI bug which has a problem with Fast Deployment.

    You can try to disable the Android "Fast deployment" options setting for the sample project.