AppResources.ResourceManager 崩溃并显示 FileNotFound 异常。

huangapple go评论124阅读模式
英文:

AppResources.ResourceManager crashes with FileNotFound-Exception

问题

In your .NET MAUI App, you are encountering multiple "FileNotFoundException" exceptions during startup. Typically, these exceptions occur when the application is unable to find the specified resources. This can happen for various reasons, and I'll provide some general suggestions to address the issue:

  1. Check Resource Files: Ensure that your .resx resource files (e.g., AppResources.resx) are correctly located and configured in your project. Verify that they are marked with the appropriate build action (e.g., "EmbeddedResource").

  2. Resource Names: Double-check that the resource keys you're using to access localized strings in your code match the actual resource keys defined in your .resx files. Typos in resource keys can cause "FileNotFoundException" errors.

  3. CultureInfo Setup: Verify that the culture and localization settings in your app are correctly configured. Make sure that the culture you're trying to set during startup is valid and available in your resource files.

  4. Resource Manager: Ensure that the Resource Manager (e.g., AppResources.ResourceManager) is correctly initialized and that it points to the correct assembly where your resources are embedded.

  5. Resource Access: During startup, consider accessing resources more defensively. You can use try-catch blocks to handle potential exceptions gracefully, log the missing resource keys, or provide default values if a resource is missing.

  6. Emulator Configuration: Ensure that your emulator's configuration and permissions are set up correctly, so it can access the resources.

  7. Clean and Rebuild: Sometimes, build issues or cached data can lead to such exceptions. Try cleaning and rebuilding your project to ensure everything is up to date.

  8. Debugging: If the issue persists, use debugging techniques to inspect the state of your resource manager and resource files during startup. This can help pinpoint the exact cause of the exceptions.

By carefully reviewing these aspects of your implementation and addressing any discrepancies, you should be able to resolve the "FileNotFoundException" exceptions during the startup of your .NET MAUI app.

英文:

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?

答案1

得分: 0

这是包中尚未解决的问题,开发者正在尝试找到答案。您可以关注以获取更多信息。

此外,有人提到这似乎是一个与Fast Deployment有关的 MAUI bug 问题。

您可以尝试禁用示例项目的 Android "Fast deployment" 选项设置。

英文:

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.

huangapple
  • 本文由 发表于 2023年5月14日 17:27:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定