[英]Blazor WebAssembly cannot load resources files from class library
I'm currently working on a Blazor WebAssembly Asp.NET hosted in .NET Standard 2.1.我目前正在研究托管在 .NET 标准 2.1 中的 Blazor WebAssembly Asp.NET。
I try to load resources files from a separate .NET Standard 2.1.我尝试从单独的 .NET 标准 2.1 加载资源文件。 C# Class library.
C# Class 库。
Unfortunately I always get the following error:不幸的是,我总是收到以下错误:
"Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Could not load type of field 'MyNamespace.Footer:k__BackingField' (0) due to: Could not load file or assembly 'MyNamespace.Resources, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies." “Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] 未处理的异常渲染组件:无法加载字段类型'MyNamespace.Footer:k__BackingField'(0),原因是:无法加载文件或程序集'MyNamespace.Resources, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null' 或其依赖项之一。"
My project structure looks like this:我的项目结构如下所示:
I load the resources like this:我像这样加载资源:
In my Program.cs Main Method I set the current culture like this:在我的 Program.cs Main Method 中,我将当前文化设置为:
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US");
and I add the Localization service:我添加了本地化服务:
services.AddLocalization();
Further on in a.razor page where I want to use the culture setting I inject the Localization service:进一步在 a.razor 页面中,我想使用文化设置注入本地化服务:
@inject IStringLocalizer<Translations> Translations
Here I simply load the project reference to my resources project.在这里,我只是将项目引用加载到我的资源项目中。 I don't use the.resx in this project.
我不在这个项目中使用.resx。
Do you know how to load resources from an external .NET Standard 2.1.你知道如何从外部 .NET Standard 2.1 加载资源吗? C# Class library into a Blazor Wasm project?
C# Class 库到 Blazor Wasm 项目?
The error comes from a name clash, the project can be compiled but not loaded.错误来自名称冲突,项目可以编译但无法加载。
I know a way to get it working, I'm not sure is this is the only or best way.我知道一种让它工作的方法,我不确定这是唯一还是最好的方法。
MyNamespace.BlazorApp.Translations
MyNamespace.BlazorApp.Translations
Resources
in the Translations projectResources
的文件夹builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
Resources is a default name at several places, not a good idea to name your own project the same. Resources 是多个地方的默认名称,将您自己的项目命名为相同名称不是一个好主意。 The ResourcesPath seems to be required, I don't know what the defaults are.
ResourcesPath 似乎是必需的,我不知道默认值是什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.