Blazor说名称空间'__Blazor ...'

时间:2019-05-04 22:30:08

标签: c# generics razor blazor

我正在玩Blazor,并尝试创建一个小的Grid组件。我想通过一个简单的对象进行配置。编译器将引发以下错误:

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?)

剃须刀和cs文件位于同一目录中。

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

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

我在这里做什么错了?

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

@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

using System.Collections.Generic;

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

修复尝试1:

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

@using相应地由Rider修复,如下例所示。

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

@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; }

}

结果

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?)

修复尝试2:

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

@using相应地由Rider修复,如下例所示。

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

@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; }

}

结果

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?)

更新

  • 似乎我想要的东西不同于Blazor / Razor可以做的。 GridComponent可以将config对象作为参数。但是,如果我想将相同的配置对象传递给GridHeader组件,则会出现编译错误。我需要更深入地研究。

1 个答案:

答案 0 :(得分:0)

您应该有一个名为 Pages 的文件夹,用于存储组件文件。不要在此文件夹中存储其他类型的文件。

定义一个名为Grid的文件夹,您应将GridComponentConfig.cs文件移入其中

在您的GridComponent.razor文件中,在页面顶部添加@using Spa.Shared.Components.Grid。

希望这对您有帮助...