英文:
How to render manually a component in Blazor?
问题
这个错误是由于在Blazor Web Assembly
中的渲染期间出现了堆栈空异常。通常,这种异常是由于在渲染组件时,没有正确打开和关闭组件或元素所导致的。
在你的代码中,特别是在CreateComponent
方法中,你使用了builder.OpenComponent
和builder.CloseComponent
,以及builder.CloseElement
方法来打开和关闭组件和元素。错误信息指出在渲染期间堆栈为空,这可能是由于未正确匹配的打开和关闭操作引起的。
要解决这个问题,你应该仔细检查CreateComponent
方法中的打开和关闭操作,确保每个OpenComponent
都有对应的CloseComponent
,每个OpenElement
都有对应的CloseElement
。你可能需要调整Increment
变量的使用来确保为每个打开操作分配一个唯一的序列号。
此外,你还应该确保CoreMenuItem
和CoreEmpty
组件的定义中,也没有导致堆栈不匹配的打开和关闭操作。检查所有涉及渲染的组件,以确保它们的渲染操作正确匹配。
通过仔细检查和纠正这些问题,你应该能够解决"Stack empty"错误。
英文:
I am trying to render manually a set of components in Blazor Web Assembly
, however I am having trouble with Attributes
and ChildContent
.
Here's how I render manually a component:
private RenderFragment CreateComponent(List<cMenuItem> menuItems) => builder =>
{
try
{
builder.OpenComponent(0, typeof(CoreEmpty));
builder.AddAttribute(1, "ChildContent", (RenderFragment)((builder2) => {
for (var i = 0; i < menuItems.Count; i++)
{
builder2.OpenComponent(Increment++, typeof(CoreMenuItem));
builder2.AddAttribute(Increment++, "Text", menuItems[i].mText);
if (menuItems[i].mItems.Count > 0) CreateComponent(menuItems[i].mItems);
builder2.CloseComponent();
}
}));
builder.CloseComponent();
builder.CloseElement();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
};
But I render it when I initialize the main component:
protected override void OnInitialized()
{
base.OnInitialized();
// Set the only menu if there is only one
if (Data != null )
if(Data.Count == 1)
{
Menu1 = Data[0];
CustomRender = CreateComponent(Menu1.mMenu);
}
}
Here's my Razor
page:
@inherits cCoreMenu
@if(Data.Count > 1)
{
@foreach(cMenu menu in Data)
{
}
}else if(Data.Count == 1)
{
<RadzenMenu Click="@Menu1.OnClickMenu">
@CustomRender
</RadzenMenu>
}
CoreEmpty
is just a dummy component because I need a component to generate a ChildContent attribute.
It gives me the following errors:
blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Stack empty.
System.InvalidOperationException: Stack empty.
at System.Collections.Generic.Stack`1[[System.Int32, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].ThrowForEmptyStack()
at System.Collections.Generic.Stack`1[[System.Int32, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Pop()
at Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.CloseRegion()
at Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.AddContent(Int32 sequence, RenderFragment fragment)
at Microsoft.AspNetCore.Components.CascadingValue`1[[Radzen.Blazor.RadzenMenu, Radzen.Blazor, Version=4.7.4.0, Culture=neutral, PublicKeyToken=null]].Render(RenderTreeBuilder builder)
at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)
What is this Stack empty
error? How can I solve it?
答案1
得分: 1
"Stack empty"错误在Pop()中只会由Close()方法之一引起。
你从未打开一个元素,所以解决方案是
// builder.CloseElement();
英文:
A Stack empty error in a Pop() can only come from one of the Close() methods.
You never Open an Element so the solution is
// builder.CloseElement();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论