简体   繁体   English

在 blazor 中添加全局使用指令

[英]Adding Global Using Directive in blazor

The type or namespace name 'XXX' does not exist in the namespace 'MyClassLibrary.Models' (are you missing an assembly reference?).命名空间“MyClassLibrary.Models”中不存在类型或命名空间名称“XXX”(您是否缺少程序集引用?)。 The error refer to this BlazorProject\MyPager.azor.g.cs错误指的是这个 BlazorProject\MyPager.azor.g.cs

I got this error after this scenario:在这种情况下我得到了这个错误:

  1. Created a file of type .cs inside my "Blazor project" and I added all my global directives by using keyword global inside this file.在我的“Blazor 项目”中创建了一个.cs类型的文件,并通过在该文件中使用关键字global添加了所有全局指令。
  2. Changed some namespaces in my "Class library"更改了我的“类库”中的一些命名空间

Structure of the project:项目结构:

  1. Blazor-serve-side project using the class library使用 class 库的 Blazor 服务端项目
  2. Class library. Class 库。

When I go the page, I can't see any error, the error just in VS, I tried to clean the solution, rebuild, closing VS and reopen but it doesn't solve the problem.当我 go 页面时,我看不到任何错误,错误只是在 VS 中,我试图清理解决方案,重建,关闭 VS 并重新打开,但它没有解决问题。 I am using VS22 and.Net6.我正在使用 VS22 和.Net6。

Note I have also in my blazor project _Imports.razor file注意我的 blazor 项目中也有_Imports.razor文件

@MisterMagoo, Thanks a lot. @MisterMagoo,非常感谢。 what you said works fine, plus that I have to repeat the same namespaces with global directive file and also in _Imports.razor file, so the soultion:你说的很好,加上我必须在global directive文件和_Imports.razor文件中重复相同的命名空间,所以灵魂:

  • In GlobalUsings.cs :GlobalUsings.cs

     global using Microsoft.AspNetCore.Components; global using Microsoft.AspNetCore.Components.Forms;
  • In _Imports.razor if you need the same namespaces in your razor pages, then you have to repeat them._Imports.razor ,如果您在 razor 页面中需要相同的命名空间,那么您必须重复它们。

     using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Forms;

Keep in mind that the scope of "global using" isn't truly global.请记住,“全局使用”的 scope 并不是真正的全局。 It's the current compilation, which typically means the current project ( not the current solution ).它是当前编译,通常表示当前项目不是当前解决方案)。 If you want to use this feature in two different projects in your solution (a Blazor Server project and a Class Library in your case, for example) you will need to create "global using" statements in both projects.如果您想在解决方案的两个不同项目中使用此功能(例如,Blazor 服务器项目和 Class 库),则需要在两个项目中创建“全局使用”语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM