简体   繁体   English

Blazor说类型或名称空间名称在名称空间“ __Blazor…”中不存在

[英]Blazor says type or namespace name does not exist in the namespace '__Blazor…'

I'm playing around with Blazor and trying to create a small Grid component. 我在玩Blazor,并尝试创建一个小的Grid组件。 I'd like to pass through configurations via a simple object. 我想通过一个简单的对象进行配置。 The compiler throws the following error: 编译器将引发以下错误:

GridComponent.razor.g.cs(61, 254): [CS0234] The type or namespace name 'GridComponentConfig' does not exist in the namespace '__Blazor.Spa.Shared.Components.Grid' (are you missing an assembly reference?)

The razor and cs files are in the same directory. razor和cs文件位于同一目录中。

<GridComponent data=@modules TItem="Module" Config=@GridComponentConfig></GridComponent>

@functions
{   
    protected GridComponentConfig GridComponentConfig = new GridComponentConfig
    {
        Columns = new List<string>
        {
            "id", "name", "description", "moduleroute"
        }
    };
}

What am I doing wrong here? 我在这里做错了什么?

GridComponent.razor GridComponent.razor

@using Microsoft.AspNetCore.Components
@typeparam TItem

<table>
    <thead>
    <GridHeader Data=@Data Config=@Config></GridHeader>
    </thead>
    <tbody>
    </tbody>
    <tfoot></tfoot>
</table>

@functions
{

    [Parameter]
    public IEnumerable<TItem> Data { get; set; }

    [Parameter]
    public GridComponentConfig Config { get; set; }

}

GridHeader.razor GridHeader.razor

@using Microsoft.AspNetCore.Components
@typeparam TItem

<tr>
    @foreach (string column in Config.Columns)
    {
        <td>@column</td>
    }
</tr>

@functions
{

    [Parameter]
    public IEnumerable<TItem> Data { get; set; }

    [Parameter]
    public GridComponentConfig Config { get; set; }

}

GridComponentConfig.cs GridComponentConfig.cs

using System.Collections.Generic;

namespace Spa.Shared.Components.Grid
{
    public class GridComponentConfig
    {
        public IEnumerable<string> Columns { get; set; }
    }
}

Fix attempt 1: 解决尝试1:

Pages/Components/Grid/GridComponent.razor
Pages/Components/Grid/GridHeader.razor
Common/GridComponentConfig.cs

The @using s fixed accordingly, example below, by Rider. @using相应地由Rider修复,例如下面的示例。

GridComponent.razor GridComponent.razor

@using Microsoft.AspNetCore.Components
@using Spa.Common
@typeparam TItem

<table>
    <thead>
    <GridHeader Data=@Data Config=@Config></GridHeader>
    </thead>
    <tbody>
    </tbody>
    <tfoot></tfoot>
</table>

@functions
{

    [Parameter]
    protected IEnumerable<TItem> Data { get; set; }

    [Parameter]
    protected GridComponentConfig Config { get; set; }

}

GridHeader.razor GridHeader.razor

@using Microsoft.AspNetCore.Components
@using Spa.Common
@typeparam TItem

<tr>
    @foreach (string column in Config.Columns)
    {
        <td>@column</td>
    }
</tr>

@functions
{

    [Parameter]
    public IEnumerable<TItem> Data { get; set; }

    [Parameter]
    public GridComponentConfig Config { get; set; }

}

Result : 结果

GridComponent.razor.g.cs(57, 231): [CS0234] The type or namespace name 'Common' does not exist in the namespace '__Blazor.Spa' (are you missing an assembly reference?)

Fix attempt 2: 解决尝试2:

Pages/Components/Grid/GridComponent.razor
Pages/Components/Grid/GridHeader.razor
Pages/Components/Grid/GridComponentConfig.cs

The @using s fixed accordingly, example below, by Rider. @using相应地由Rider修复,例如下面的示例。

GridComponent.razor GridComponent.razor

@using Microsoft.AspNetCore.Components
@typeparam TItem

<table>
    <thead>
    <GridHeader Data=@Data Config=@Config></GridHeader>
    </thead>
    <tbody>
    </tbody>
    <tfoot></tfoot>
</table>

@functions
{

    [Parameter]
    protected IEnumerable<TItem> Data { get; set; }

    [Parameter]
    protected GridComponentConfig Config { get; set; }

}

GridHeader.razor GridHeader.razor

@using Microsoft.AspNetCore.Components
@typeparam TItem

<tr>
    @foreach (string column in Config.Columns)
    {
        <td>@column</td>
    }
</tr>

@functions
{

    [Parameter]
    public IEnumerable<TItem> Data { get; set; }

    [Parameter]
    public GridComponentConfig Config { get; set; }

}

Result : 结果

GridComponent.razor.g.cs(56, 253): [CS0234] The type or namespace name 'GridComponentConfig' does not exist in the namespace '__Blazor.Spa.Pages.Components.Grid' (are you missing an assembly reference?)

Update : 更新

  • it seems I want something way different than Blazor/Razor can do. 似乎我想要的东西不同于Blazor / Razor可以做到的。 GridComponent can take the config object as parameter. GridComponent可以将config对象作为参数。 But, if I want to pass the same config object to GridHeader component, then the compile error arises. 但是,如果我想将相同的配置对象传递给GridHeader组件,则会出现编译错误。 I need to dig deeper. 我需要深入研究。

You should have a folder named Pages in which you store your components files. 您应该有一个名为Pages的文件夹,用于在其中存储组件文件。 Do not store other types of files in this folder. 不要在此文件夹中存储其他类型的文件。

Define a folder named Grid into which you should move the GridComponentConfig.cs file 定义一个名为Grid的文件夹,您应将GridComponentConfig.cs文件移入其中

In your GridComponent.razor file add @using Spa.Shared.Components.Grid at the top of the page. 在您的GridComponent.razor文件中,在页面顶部添加@using Spa.Shared.Components.Grid。

Hope this helps... 希望这可以帮助...

暂无
暂无

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

相关问题 Blazor:命名空间“Microsoft.AspNetCore.Mvc.ApplicationParts”中不存在类型或命名空间名称“ApplicationPartAttributeAttribute” - Blazor: The type or namespace name 'ApplicationPartAttributeAttribute' does not exist in the namespace 'Microsoft.AspNetCore.Mvc.ApplicationParts' 名称在名称空间中不存在,但intellisense表示确实存在 - The name does not exist in the namespace but intellisense says it does 命名空间中不存在类型或命名空间名称“Exchange” - The type or namespace name 'Exchange' does not exist in the namespace 类型或名称空间名称“ Confidence”在名称空间中不存在 - The type or namespace name 'Confidence' does not exist in the namespace 命名空间中不存在类型或命名空间名称 - type or namespace name does not exist in the namespace 命名空间中不存在类型或命名空间名称“优化” - The type or namespace name 'Optimization' does not exist in the namespace 类型或名称空间名称在名称空间中不存在 - The type or namespace name does not exist in the namespace 名称空间中不存在类型或命名空间名称“GetName” - The type or namespace name 'GetName' does not exist in the namespace 类型或名称空间名称“ MIDI”在名称空间中不存在 - The type or namespace name 'MIDI' does not exist in the namespace 命名空间中不存在类型或命名空间名称“Linq” - The type or namespace name 'Linq' does not exist in the namespace
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM