简体   繁体   English

CSS 隔离用于 Razor 组件,该组件完全由 C# ZA2F2ED4F8EBC2CBB1D6C21A29DC40ABC 定义

[英]CSS isolation for a Razor component which is fully defined with a C# class

I have a Razor component, called RankingCell , which is fully defined with a C# class.我有一个 Razor 组件,称为RankingCell ,它完全由 C# class 定义。 There is not any .razor file associated to it :没有任何.razor文件与之关联

// RankingCell.razor.cs
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;

namespace MyProj {
    public class RankingCell: ComponentBase {
        // Some parameters and properties

        [Parameter]
        public string CellElt { get; set; }

        protected override void BuildRenderTree(RenderTreeBuilder builder) {
            /*
             * The thing which prevents me from using a "RankingCell.razor" file to define it.
             * In a .razor file it would be something like this but it is not possible:
             * 
             * <@CellElt ... >@* ... *@</@CellElt>
             * 
             * Please have a look there for for details about how I solved this issue for the component: 
             * https://stackoverflow.com/questions/64196493/in-blazor-how-can-i-dynamically-change-an-html-tag
             */
            builder.OpenElement(0, CellElt);

            // ...

            builder.CloseElement();
        }
    }
}

I would like to associate a stylesheet to my RankingCell component, so I create such a file following Microsoft's documentation about CSS isolation , ie a CSS file called RankingCell.razor.css : I would like to associate a stylesheet to my RankingCell component, so I create such a file following Microsoft's documentation about CSS isolation , ie a CSS file called RankingCell.razor.ZC7A62 RankingCell.razor.css :

/* RankingCell.razor.css */
.ranking-cell {
    /* RankingCell style */
}

But when I build my project (using dotnet build ), I have got the following error:但是当我构建我的项目(使用dotnet build )时,出现以下错误:

RankingCell.razor.css : error BLAZOR102: The scoped css file 'RankingCell.razor.css' was defined but no associated razor component was found for it.

How to fix this BLAZOR102 issue?如何解决此 BLAZOR102 问题? How to manage to associate a stylesheet with my component?如何设法将样式表与我的组件相关联?

The component does not look buggy.该组件看起来没有问题。 I have not got any compiling error when no CSS scoped file is associated to it and it works in my Blazor project.当没有 CSS 范围文件与之关联并且它在我的 Blazor 项目中工作时,我没有遇到任何编译错误。

I also tried to create an empty RankingCell.razor file but I had got another which told me that BuildRenderTree was defined elsewhere (of course since it is already fully written in RankingCell.razor.cs , where the component is fully defined).我还尝试创建一个空的RankingCell.razor文件,但我有另一个文件告诉我BuildRenderTree是在其他地方定义的(当然,因为它已经完全写在RankingCell.razor.cs中,其中组件已完全定义)。

You need to add "partial" to RankingCell.razor.cs like this:您需要将“部分”添加到 RankingCell.razor.cs,如下所示:

public partial class RankingCell: ComponentBase{
}

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

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