如何在Blazor中手动渲染一个组件?

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

How to render manually a component in Blazor?

问题

这个错误是由于在Blazor Web Assembly中的渲染期间出现了堆栈空异常。通常,这种异常是由于在渲染组件时,没有正确打开和关闭组件或元素所导致的。

在你的代码中,特别是在CreateComponent方法中,你使用了builder.OpenComponentbuilder.CloseComponent,以及builder.CloseElement方法来打开和关闭组件和元素。错误信息指出在渲染期间堆栈为空,这可能是由于未正确匹配的打开和关闭操作引起的。

要解决这个问题,你应该仔细检查CreateComponent方法中的打开和关闭操作,确保每个OpenComponent都有对应的CloseComponent,每个OpenElement都有对应的CloseElement。你可能需要调整Increment变量的使用来确保为每个打开操作分配一个唯一的序列号。

此外,你还应该确保CoreMenuItemCoreEmpty组件的定义中,也没有导致堆栈不匹配的打开和关闭操作。检查所有涉及渲染的组件,以确保它们的渲染操作正确匹配。

通过仔细检查和纠正这些问题,你应该能够解决"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();

huangapple
  • 本文由 发表于 2023年3月3日 23:01:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628675.html
匿名

发表评论

匿名网友

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

确定