问题描述
我正在学习Blazor. https://learn-blazor.com/
I am learning Blazor.https://learn-blazor.com/
通过覆盖BlazorComponent
类的BuildRenderTree(RenderTreeBuilder builder)
方法,我在用C#创建组件时遇到了一些困难.
I am having some difficulty creating a component in C# by overriding the BuildRenderTree(RenderTreeBuilder builder)
method of the BlazorComponent
class.
这是我的课程:
public class TestComponent : BlazorComponent
{
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenElement(1, "p");
builder.OpenElement(2, "strong");
builder.AddContent(3, "hello");
builder.CloseElement();
builder.CloseElement();
base.BuildRenderTree(builder); // With or without this line it doesn't work
}
}
我在页面中这样使用它:
I use it in my page like this:
<TestComponent></TestComponent>
我已经测试了是否有最后一行base.BuildRenderTree(builder);
,但是无论哪种方式都无法渲染.我已经使该组件幼稚地变得简单了(只是一个段落标签,强标签和一个内容词),所以我迷失了为什么它无法呈现.代码构建良好.
I have tested it with and without that last line base.BuildRenderTree(builder);
but either way it doesn't render. I've made the component childishly simple (just a paragraph tag, strong tag and one word of content) so I'm lost as to why it won't render. The code builds just fine.
任何人都可以看到我做错了吗?
Can anybody see what I did wrong please?
推荐答案
首先不要再使用该网站.我已经通过该网站学习了Blazor.这是一个出色的网站,这家伙做得很出色.但是可惜的是,至少在过去六个月中,他停止了对其进行更新.材料已经陈旧无用.目前,我还没有任何好的替代方法.
First off don't use that web site anymore. I've learned Blazor through that web site. It is an excellent web site, and the guy did a superb work. But alas, he stopped updating it, at least for the last six months. The materials have become old and useless. Right now, I'm not aware of any good substitution.
尝试一下:
-
将
base.BuildRenderTree(builder);
放置在BuildRenderTree方法的开始处,而不是在结尾处.
Place
base.BuildRenderTree(builder);
at the start of the BuildRenderTree method , not at the end.
使用ComponentBase
代替BlazorComponent
...BlazorComponent已死...
Use ComponentBase
instead of BlazorComponent
... BlazorComponent is dead...
这篇关于通过重写BlazorComponent.BuildRenderTree(RenderTreeBuilder builder)创建组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!