简体   繁体   English

Blazor WebAssembly 无法从 class 库加载资源文件

[英]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' 或其依赖项之一。"

  1. My.resx files are set to Access Modifier: Public and look like this in the properties tab: My.resx 文件设置为Access Modifier: Public并在属性选项卡中如下所示:

在此处输入图像描述

My project structure looks like this:我的项目结构如下所示:

  • MyNamespace.BlazorApp.Client MyNamespace.BlazorApp.Client
  • MyNamespace.BlazorApp.Server MyNamespace.BlazorApp.Server
  • MyNamespace.BlazorApp.Resources MyNamespace.BlazorApp.Resources

I load the resources like this:我像这样加载资源:

  1. MyNamespace.BlazorApp.Client: MyNamespace.BlazorApp.Client:

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
  1. MyNamespace.BlazorApp.Server MyNamespace.BlazorApp.Server

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.我知道一种让它工作的方法,我不确定这是唯一还是最好的方法。

  1. Rename your resx project to MyNamespace.BlazorApp.Translations将您的 resx 项目重命名为MyNamespace.BlazorApp.Translations
  2. Make sure there is a public class Translation in the root (no 's')确保根目录中有公共 class 翻译(没有's')
  3. Make a Folder called Resources in the Translations project在 Translations 项目中创建一个名为Resources的文件夹
  4. Add Translation.resx, Translation.es.resx etc to that folder将 Translation.resx、Translation.es.resx 等添加到该文件夹
  5. Change the config to 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.

相关问题 从 Blazor Webassembly 流式传输大文件? - Streaming Large Files from Blazor Webassembly? Blazor 无法在发布时从 class 库加载图像 - Blazor can't load image from class library at publish wpf类库中的WPF ToastNotifications无法加载资源 - WPF ToastNotifications from wpf class library does not load Resources 将变量声明从 Blazor 页面分离到 Blazor WebAssembly 应用程序中的数据存储库中的 Class - Separating Variable Declarations from a Blazor page into a Class in Data repository in a Blazor WebAssembly App Blazor WebAssembly 为特定环境加载不同的脚本 - Blazor WebAssembly load different scripts for specific Environment 如何加载 ResponseCompressionDefaults function(Blazor WebAssembly SignalR) - how to load ResponseCompressionDefaults function (Blazor WebAssembly SignalR) 在磁盘上找到 Blazor Webassembly 下载的 .dll 文件? - Locate Blazor Webassembly downloaded .dll files on disk? C# Blazor WebAssembly:参数 2:无法从“void”转换为“Microsoft.AspNetCore.Components.EventCallback” - C# Blazor WebAssembly: Argument 2: cannot convert from 'void' to 'Microsoft.AspNetCore.Components.EventCallback' 为什么 blazor 从 class 库加载页面时会加载其他文件 - Why is blazor loading additional files when loading a page from a class library Blazor Webassembly 应用程序显示来自服务器的图像 - Blazor Webassembly App display image from server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM