英文:
Blazor render dynamic Markup with Components inside
问题
我正在尝试在其中使用Blazor
组件呈现一些动态HTML。<br />
例如:
<div>
文本
<Component 属性1="A1" />
</div>
现在,这个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.:
<div>
Text
<Component Attribute1="A1" />
</div>
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:
<div>
Text
<Component Attribute1="A1" />
</div>
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, "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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论