Is it possible to get a list of RenderFragments when overriding BuildRenderTree?

 ̄綄美尐妖づ 提交于 2019-12-02 08:33:38

This is certainly not possible, at least not in the foreseeable future. The RenderFragment represents a chunk of content to render, the content of which, as far as I know, and for the time being, cannot be access programmatically, the way you want...

In Blazor, and in life as well, the flow of data is from a parent to a child component. I guess this is why one must design his software in accordance with this "inherent" limitation. In principal I believe anything is achievable, depending on, Dan Roth would say, resources available.

What's about Templated Components ? With Templated Component, your UIList component knows and determine the number of UIListItems to render...

Incidentally, I guess this is the only added value I can provide in this answer: You are not using the sequence properly. Steve Anderson has written an article about this; search for it).You shouldn't use an incrementing variable to issue the sequence. You should use numbers instead, starting with 0.

Hope this helps...

As Issac states they way you are attempting to solve your problem is not possible with Blazor. However, it is possible for child elments to register themselves with a parent using a CascadingParameter. This seems like it would achieve what you want it's just coming at it from the other direction. Let me try an example...

Given this setup

<ParentComponent>
    <ChildComponent></ChildComponent>
    <ChildComponent></ChildComponent>
    <ChildComponent></ChildComponent>
</ParentComponent>

I think you looking for an output like this

<div>
    <p>Total Items: 3</p>
    <div>Child Component</div>
    <div>Child Component</div>
    <div>Child Component</div>
</div>

Parent Component

<div>
    <p>Total Items: @ItemCount</p>
    <CascadingValue Value="this">
        @ChildContent
    </CascadingValue>
</div>

@functions {

    private int ItemCount;

    public void AddItem()
    {
        ItemCount++;
        StateHasChanged();
    }

}
<div>Child Component</div>

@functions {

    [CascadingParameter] private ParentComponet Parent { get; set; }

    protected override void OnInit()
    {
        Parent.AddItem();
    }

}

This should achieve what you're looking for if I understood your need correctly.

One thing I would say is try and stay away from overriding BuildRenderTree, I've written a blog post about how to do it correctly but also point out why you probably shouldn't.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!