Blazor使用组件在内部呈现动态标记。

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

Blazor render dynamic Markup with Components inside

问题

我正在尝试在其中使用Blazor组件呈现一些动态HTML。<br />
例如:

&lt;div&gt;
    文本
    &lt;Component 属性1="A1" /&gt;
&lt;/div&gt;

现在,这个HTML片段来自数据库而不是代码,所以我正在寻找一种方法来强制Blazor重新渲染内容并将组件解析为其HTML表达式

我已经尝试过使用RenderFragment,但那不起作用。
我还了解了使用RenderTreeBuilder,但那是非常低级的,我需要将其细化到一个程度,以至于使用RegularExpression并将组件替换为传入HTML中的预定义占位符加载的HTML会更容易。

有没有人知道更简单的方法?或者一种强制Blazor重新评估传入的HTML/Razor/Blazor字符串的方法?

英文:

I am trying to render some dynamic HTML with Blazor Components in them.<br />
E.g.:

&lt;div&gt;
    Text
    &lt;Component Attribute1=&quot;A1&quot; /&gt;
&lt;/div&gt;

Now, this HTML Snippet comes from a database and not from Code, so I am looking for some way to force Blazor to (re?-) render the content and resolve the Component into its HTML-Expression.

I already tried something with RenderFragment, but that did not work.
I also read about using the RenderTreeBuilder, but that is so very low-level, I would need to make it so granular, that it would be easier to use a RegularExpression and replace the component in the incoming HTML with some predefined placeholder-laden-HTML.

Does anyone know an easier way? Or some way to force Blazor to reevaluate the incoming HTML/Razor/Blazor-String?

答案1

得分: 1

以下是翻译好的部分:

Does anyone know an easier way? Or some way to force Blazor to re-evaluate the incoming HTML/Razor/Blazor-String?

有没有人知道更简单的方法?或者有一些方法可以强制 Blazor 重新评估传入的 HTML/Razor/Blazor 字符串?

Straight answer is not possible. You can't store markup code and somehow feed it into the Renderer at runtime.

直接回答是不可能的。你不能在运行时存储标记代码,然后以某种方式将其传递给渲染器。

Why?

为什么?

Markup code, such as:

标记代码,例如:

<div>
    Text
    <Component Attribute1="A1" />
</div>

is compiled by the Razor compiler into a standard C# class that produces code like this:

Razor 编译器将标记代码编译成一个标准的 C# 类,生成类似以下代码:

protected override void BuildRenderTree(RenderTreeBuilder builder)
{
    //...
    builder.OpenElement(0, "div");
    builder.AddMarkupContent(1, "Text");
    builder.OpenComponent<Counter>(2);
    builder.AddAttribute(3, "Attribute1", "A1");
    builder.CloseComponent();
    builder.CloseElement();
    //...
}

It's part of the compilation process, not a runtime option.

这是编译过程的一部分,而不是运行时选项。

英文:

> Does anyone know an easier way? Or some way to force Blazor to re-evaluate the incoming HTML/Razor/Blazor-String?

Straight answer is not possible. You can't store markup code and somehow feed it into the Renderer at runtime.

Why?

Markup code, such as:

&lt;div&gt;
    Text
    &lt;Component Attribute1=&quot;A1&quot; /&gt;
&lt;/div&gt;

is compiled by the Razor compiler into a standard C# class that produces code like this:

    protected override void BuildRenderTree(RenderTreeBuilder builder)
    {
        //...
        builder.OpenElement(0, &quot;div&quot;);
        builder.AddMarkupContent(1, &quot;Text&quot;);
        builder.OpenComponent&lt;Counter&gt;(2);
        builder.AddAttribute(3, &quot;Attribute1&quot;, &quot;A1&quot;);
        builder.CloseComponent();
        builder.CloseElement();
        //...
    }

It's part of the compilation process, not a runtime option.

huangapple
  • 本文由 发表于 2023年2月6日 21:36:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75362011.html
匿名

发表评论

匿名网友

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

确定