获取 Blazor 组件的 CssScope 值

huangapple go评论48阅读模式
英文:

Obtain CssScope value of a Blazor Component

问题

I have a Blazor server application that contains a component which generates a list of html paragraphs.

<div class="text">
  @FormatText(Obj.Synposis)
</div>

using this simple function:

@functions 
{
  private MarkupString FormatText(string text)
  {
    var Result = string.Concat(text.Split('¶', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).Select(e => $@"<p>{e}</p>"));

    return new MarkupString(Result);
  }
}

the corresponding component's css style contains this:

  div.text > p {
    margin-bottom: 50rem;
    background-color: red;
  }

This issue is that the generated html string lacks the component's css scope value and therefore any style applied is ignored.

To make sure we're on the same page, the rendered css will contain this:

  div.text > p[b-taj2ej35sx] {
    margin-bottom: 50rem;
    background-color: red;
  }

... and I would have to apply that css scope as <p b-taj2ej35sx="">... in FormatText() for the style to get applied. I need to obtain the string value "b-taj2ej35sx" somehow.

The question is:

  1. can I (and how) find the component's css scope value? can I re-calculate the value myself (the code following b-)? then it would be just a matter of adding that to the <p> string
  2. can I tell blazor to not apply css scoping for a single css element in the style sheet?
  3. can I trick blazor somehow to fail to scope a css element? it already looks like the compiler failed to scope both div.text and p elements in that style rule
英文:

I have a Blazor server application that contains a component which generates a list of html paragraphs.

<div class="text">
  @FormatText(Obj.Synposis)
</div>

using this simple function:

@functions 
{
  private MarkupString FormatText(string text)
  {
    var Result = string.Concat(text.Split('¶', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).Select(e => $@"<p>{e}</p>"));

    return new MarkupString(Result);
  }
}

the corresponding component's css style contains this:

  div.text > p {
    margin-bottom: 50rem;
    background-color: red;
  }

This issue is that the generate html string lacks the component's css scope value and therefore any style applied is ignored.

To make sure we're on the same page, the rendered css will contain this:

  div.text > p[b-taj2ej35sx] {
    margin-bottom: 50rem;
    background-color: red;
  }

... and I would have to apply that css scope as <p b-taj2ej35sx="">... in FormatText() for the style to get applied. I need to obtain the string value "b-taj2ej35sx" somehow.

The question is:

  1. can I (and how) find the component's css scope value? can I re-calculate the value myself (the code following b-)? then it would be just a matter of adding that to the <p> string
  2. can I tell blazor to not apply css scoping for a single css element in the style sheet?
  3. can I trick blazor somehow to fail to scope a css element? it already looks like the compiler failed to scope both div.text and p elements in that style rule

答案1

得分: 0

不需要处理范围唯一标识符。我假设你的所有代码都在同一个组件中。

以下是演示你所设定的并且有效的代码。我稍微简化了它。

关键是使用 RenderFragments。Razor 编译器将 MarkupString 视为普通文本,不会应用任何 CSS 组件魔法。

Index.razor 中的生成的标记部分:

@page "/";

<PageTitle>Index</PageTitle>

<h1>Hello, world!</h1>

<div class="text">
    @GetFormattedText
</div>

@code {
    private string countries = "France, UK, Spain, Portugal";

    private RenderFragment GetFormattedText => (__builder) =>
    {
        var items = countries.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
        foreach(var item in items)
        {
            <p>@item</p>
        }
    };
}

Index.razor.css

div.text > p {
    margin-bottom: 5rem;
    background-color: red;
}

结果如下图所示:

获取 Blazor 组件的 CssScope 值

英文:

You shouldn't need to do anything with the scope unique Id.

I'm assuming all your code is in the same component.

Here's my code that demonstrates what you've set out and works. I've simplified it a little.

The key is to use RenderFragments. The Razor compiler treats MarkupString as just that, and doesn't apply any CSS component magic to it.

Index.razor with the markup that should be generated.

@page &quot;/&quot;

&lt;PageTitle&gt;Index&lt;/PageTitle&gt;

&lt;h1&gt;Hello, world!&lt;/h1&gt;


&lt;div class=&quot;text&quot;&gt;
    @GetFormattedText
&lt;/div&gt;

@code {
    private string countries = &quot;France, UK, Spain, Portugal&quot;;

    private RenderFragment GetFormattedText =&gt; (__builder) =&gt;
    {
        var items = countries.Split(&#39;,&#39;, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
        foreach(var item in items)
        {
         &lt;p&gt;@item&lt;/p&gt;   
        }
    };
}

Index.razor.css:

div.text &gt; p {
    margin-bottom: 5rem;
    background-color: red;
}

And the result:

获取 Blazor 组件的 CssScope 值

huangapple
  • 本文由 发表于 2023年3月31日 22:40:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定