Blazor Component Documentation建议在构建自定义 RenderTree
时使用常量序列号:
Generating the sequence numbers has lost all the useful information about where the if/else branches and loops were present in the original code. This results in a diff twice as long as before. ... In more realistic cases with complex and deeply nested structures, and especially with loops, the performance cost is more severe. Instead of immediately identifying which loop blocks or branches have been inserted or removed, the diff algorithm has to recurse deeply into the render trees and usually build far longer edit scripts because it is misinformed about how the old and new structures relate to each other.
如果集合的大小无法在编译时确定,那么 Blazor 编译器如何确定枚举元素(即循环中重复的元素)的序列值?
最佳答案
下面是 Blazor 编译器如何使用定义表元素的代码编译 .razor 文件。请注意,在循环中添加的所有元素都具有相同的序列号
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenElement(0, "table");
builder.OpenElement(1, "tbody");
for (var row = 0; row < 3; row++)
{
builder.OpenElement(2, "tr");
for (var col = 0; col < 3; col++)
{
builder.OpenElement(3, "td");
builder.AddAttribute(4, "class", "tictactoe-cell");
builder.CloseElement();
}
builder.CloseElement();
}
builder.CloseElement();
builder.CloseElement();
}
}
我建议您用一些文本(“Blazor!”)定义一个简单的组件,并在另一个组件中动态实例化它,即用户输入文本框的次数。运行看看是否有效。现在转到编译器生成的 .g.cs 文件,查看编译器最初生成的内容。来告诉我们你的实验吧。
你的问题非常重要,当我们想要创建一个基于从数据库中检索的数据的菜单组件、搜索的个人资料结果列表等时,熟练掌握它是必不可少的。
希望这有助于...
关于c# - Blazor 编译器如何生成序列号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56482826/