简体   繁体   English

Blazor 找不到类型或命名空间名称“App”(是否缺少 using 指令或程序集引用?)

[英]Blazor the type or namespace name 'App' could not be found (are you missing a using directive or an assembly reference?)

I have setup the basic application in Blazor in Microsoft Visual Studio Community 2019 Version 16.1.3 and I have tried both of the versions of .NET Core SDK 3.0.100-preview5-011568 and SDK 3.0.100-preview6-012264.我已经在 Microsoft Visual Studio Community 2019 版本 16.1.3 中的 Blazor 中设置了基本应用程序,并且我已经尝试了 .NET Core SDK 3.0.100-preview5-011568 和 SDK 3.0.100222preview64 的两个版本

Also I have installed the Blazor extension but when I am building without any modification in any files, the build fails with the following error:我还安装了 Blazor 扩展,但是当我在没有对任何文件进行任何修改的情况下进行构建时,构建失败并出现以下错误:

"The type or namespace "App" could not be found. “找不到类型或命名空间“App”。

Any help will be highly appreciated.任何帮助将不胜感激。 Thanks.谢谢。

To use Blazor you need to have VS2019 preview edition or enable preview features on VS2019 by checking要使用 Blazor,您需要拥有 VS2019 预览版或通过检查启用 VS2019 上的预览功能

Tools -> Options -> Environment -> Preview Features -> Use previews of the .NET Core SDK工具 -> 选项 -> 环境 -> 预览功能 -> 使用 .NET Core SDK 的预览

or on the older versions of VS2019或者在旧版本的 VS2019 上

Tools -> Options -> Projects and Solutions -> .NET Core -> Use previews of the .NET Core SDK工具 -> 选项 -> 项目和解决方案 -> .NET Core -> 使用 .NET Core SDK 的预览版

checkbox, then reload the solution and build.复选框,然后重新加载解决方案并构建。

<component type="typeof(App)" render-mode="ServerPrerendered" />

The error I got (using Visual Studio 2019 v16.8.2 & .NET 5 in a Blazor Server App) was pointing to the "App" part of the line above (in the file _Host.cshtml).我得到的错误(在 Blazor 服务器应用程序中使用 Visual Studio 2019 v16.8.2 和 .NET 5)指向上面行的“应用程序”部分(在文件 _Host.cshtml 中)。 This happened after copying in some code from another project, and encountering issues with a different (application) namespace, I got this error (and no others) when trying to build.这发生在从另一个项目中复制一些代码之后,并且遇到不同(应用程序)命名空间的问题,我在尝试构建时遇到了这个错误(并且没有其他错误)。 @SᴇM's answer helped me solve the problem - I closed and re-opened my solution , and then got namespace errors when building - which I could resolve... after fixing that it worked. @SᴇM 的回答帮助我解决了这个问题 -我关闭并重新打开了我的解决方案,然后在构建时遇到了命名空间错误 - 我可以解决......在修复它之后。

I would have commented to @SᴇM's solution, but I don't have enough 'reputation'.我会对@SᴇM 的解决方案发表评论,但我没有足够的“声誉”。

Addition to @SᴇM answer, please make sure you are using latest version of Visual studio.除了@SᴇM 答案,请确保您使用的是最新版本的 Visual Studio。 For my case, I followed the same steps mentioned by @SᴇM but still option Blazor App option not shown.就我而言,我遵循@SᴇM 提到的相同步骤,但仍然没有显示选项Blazor App选项。

After that, I updated visual studio to 16.3.7 and now option is visible while creating new project.之后,我将 Visual Studio 更新到 16.3.7,现在选项在创建新项目时可见。 :) :)

After migrating my .NET Core 3.1 application to .NET 5.0 I experienced the same problem.将我的 .NET Core 3.1 应用程序迁移到 .NET 5.0 后,我遇到了同样的问题。 My application was working just fine but since I opened my App.razor file errors showed.我的应用程序运行良好,但自从我打开 App.razor 文件后出现错误。

"the type or namespace name 'pageName' could not be found" and the pages show normally. “找不到类型或命名空间名称'pageName'”并且页面正常显示。

Fixed the problem with Tools -> Options -> Environment -> Preview Features -> Use previews of the .NET Core SDK.修复了工具 -> 选项 -> 环境 -> 预览功能 -> 使用 .NET Core SDK 预览的问题。 Restart of Visual Studio and reloading project was also needed.还需要重新启动 Visual Studio 并重新加载项目。

Which is kind of weird since I just moved to .NET 5.0这有点奇怪,因为我刚搬到 .NET 5.0

For me, Blazor app was building locally, but not in Azure pipeline.对我来说,Blazor 应用是在本地构建的,而不是在 Azure 管道中构建的。

Adding App.razor.cs solved the build problem.添加 App.razor.cs 解决了构建问题。

using Microsoft.AspNetCore.Components;

namespace BlazorDemo.Client
{
    public partial class App : ComponentBase
    {
    }
}

Adding this line to.csproj fixed it for me.将此行添加到 .csproj 为我修复了它。

<RootNamespace>MyBlazorApp</RootNamespace>

It can also be set on the Properties page, under "Default namespace".也可以在“默认名称空间”下的“属性”页面上进行设置。

In my case the problem was that I had the following package reference in my csproj file:就我而言,问题是我的 csproj 文件中有以下包引用:

<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />

Removing this fixed the issue.删除这个解决了这个问题。

The following output in my build.log file helped me track this down我的 build.log 文件中的以下输出帮助我追踪了这一点

1>/usr/local/share/dotnet/sdk/3.1.402/Sdks/Microsoft.NET.Sdk.Razor/build/netstandard2.0/Sdk.Razor.CurrentVersion.targets(404,5): warning RAZORSDK1006: Detected Razor language version downgrade. This is typically caused by a reference to the Microsoft.AspNetCore.Razor.Design package. Consider removing this package reference.

重新启动 Visual Studio 为我工作

除了@Jonno 回答,如果您复制剃须刀文件,您需要在文件属性中将构建操作更改为内容

将我的项目从“App”重命名为“GCL-App”对我有用,因为它引发了“循环依赖问题,包括上述错误。

In my case the issue was the namespace definition in _Host.cshtml.就我而言,问题是 _Host.cshtml 中的命名空间定义。 Renaming it to the correct namespace solved the issue.将其重命名为正确的命名空间解决了这个问题。

我遇到了类似的错误,重新启动 Visual Studio 2019 为我修复了它。

For me adding the namespace with a using statement worked.对我来说,使用 using 语句添加命名空间是有效的。

@using PieShopHrmClientWebAssembly.App.Shared

Here PieShopHrmClientWebAssembly.App is my Blazor project name.这里 PieShopHrmClientWebAssembly.App 是我的 Blazor 项目名称。

This seems to be a generic message that the compilation has failed.这似乎是编译失败的通用消息。 In my case the Error List was not useful, but Build Output had more information (search for "error") but it only had a partial hint:在我的情况下,错误列表没有用,但构建输出有更多信息(搜索“错误”),但它只有部分提示:

warning CS8785: Generator 'RazorSourceGenerator' failed to generate source.警告 CS8785:生成器“RazorSourceGenerator”无法生成源。 It will not contribute to the output and compilation errors may occur as a result.它不会影响输出,因此可能会出现编译错误。 Exception was of type 'KeyNotFoundException'异常类型为“KeyNotFoundException”

It was still not enough, so I right-clicked the solution and chose "open in terminal" and there I executed this command manually:仍然不够,所以我右键单击解决方案并选择“在终端中打开”,然后我手动执行了这个命令:

dotnet build点网构建

And this one gave me some more information about the actual key not being found ("TableItem" in my case).而这个给了我一些关于没有找到实际密钥的更多信息(在我的例子中是“TableItem”)。 This led me to some trial and error and I had some conflicting libraries (custom copy of BlazorTable and a new nuget version of MudBlazor) with similar component names.这导致我进行了一些试验和错误,并且我有一些冲突的库(BlazorTable 的自定义副本和 MudBlazor 的新 nuget 版本)具有相似的组件名称。

I renamed <Table> to <BlazorTable>, and later to <BlaTable> 
(and ended up using <BlazorTable.BlaTable> to avoid repeating the namespace in class name)

Since I was still getting errors, I also removed all "@using BlazorTable" and instead included namespace in all tags, including attribute values in enum cases:由于我仍然遇到错误,我还删除了所有“@using BlazorTable”,而是在所有标签中包含命名空间,包括枚举案例中的属性值:

<BlazorTable.BlaTable TableItem="BillPlanListModel"
    Items=Rows 
    SelectionType=BlazorTable.SelectionType.Single>
        <BlazorTable.Column TableItem="BillPlanListModel" />
</BlazorTable.BlaTable>

This was a bit painful but it helped in my case after a few hours of work, and now I could upgrade MudBlazor to a new version.这有点痛苦,但经过几个小时的工作,它对我的​​情况有所帮助,现在我可以将 MudBlazor 升级到新版本。 I was using .net 6 in VS2022, and got this error when updating MudBlazor from 6.02 to 6.03 (and later I was able to update MudBlazor to 6.0.10).我在 VS2022 中使用 .net 6,在将 MudBlazor 从 6.02 更新到 6.03 时出现此错误(后来我能够将 MudBlazor 更新到 6.0.10)。

I was doing so code cleanup, including removing unneeded using statements and started getting this error.我正在这样做代码清理,包括删除不需要的 using 语句并开始出现此错误。 I (or a tool) removed the site usings from the _Imports.razor fixed it.我(或工具)从修复它的 _Imports.razor 中删除了网站使用。

Adding添加

@using {Site.Client}
@using {Site.Client}.Shared

back to my _Imports.razor fixed the problem.回到我的 _Imports.razor 解决了这个问题。

暂无
暂无

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

相关问题 找不到类型或名称空间名称(缺少使用指令或程序集引用吗?) - Type or namespace name could not be found (missing using directive or assembly reference?) 找不到类型或名称空间名称“ My_Interface_Name”(您是否缺少using指令或程序集引用?) - The type or namespace name 'My_Interface_Name' could not be found (are you missing a using directive or an assembly reference?) 找不到类型或名称空间名称“ FacebookSessionClient”(您是否缺少using指令或程序集引用?) - The type or namespace name 'FacebookSessionClient' could not be found (are you missing a using directive or an assembly reference?) 找不到类型或命名空间名称&#39;SignInManager&#39;(您是否缺少using指令或程序集引用?) - The type or namespace name 'SignInManager' could not be found (are you missing a using directive or an assembly reference?) 错误 CS0246:找不到类型或命名空间名称“IWebHostEnvironment”(您是否缺少 using 指令或程序集引用?) - error CS0246: The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?) 找不到类型或名称空间名称“ PrintDocument”(是否缺少using指令或程序集引用?) - The type or namespace name 'PrintDocument' could not be found (are you missing a using directive or an assembly reference?) 找不到类型或命名空间名称“OleDbConnection”(您是否缺少 using 指令或程序集引用?) - The type or namespace name 'OleDbConnection' could not be found (are you missing a using directive or an assembly reference?) 为什么会出现这个错误? “找不到类型或名称空间名称&#39;c&#39;(您是否缺少using指令或程序集引用?)” - Why this error ? “The type or namespace name 'c' could not be found (are you missing a using directive or an assembly reference?)” 找不到类型或命名空间名称&#39;ContractClassFor&#39;(您是否缺少using指令或程序集引用?) - The type or namespace name 'ContractClassFor' could not be found (are you missing a using directive or an assembly reference?) 错误1找不到类型或名称空间名称&#39;FPSTimer&#39;(您是否缺少using指令或程序集引用?) - Error 1 The type or namespace name 'FPSTimer' could not be found (are you missing a using directive or an assembly reference?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM