覆盖BuildRenderTree时是否可以获得RenderFragments列表? [英] Is it possible to get a list of RenderFragments when overriding BuildRenderTree?

查看:177
本文介绍了覆盖BuildRenderTree时是否可以获得RenderFragments列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能在组件的ChildContent参数中引用子元素.我可以将值从父级组件显式传递给子级,也可以使用级联参数传递给子级,但是父级组件没有知道" ChildContent RenderFragment内部元素的好方法.一个可能的用例是UIList组件,该组件获取在渲染过程中已添加到UIList组件中的UIListItems的数量.

I'm wondering if it's possible to reference child elements in the ChildContent parameter of a component. I can pass values from parent components to children explicitly or by using a cascading parameter, but there is no great way for a parent component to "know" about elements inside the ChildContent RenderFragment. A possible use-case for this would be a UIList component that gets the number of UIListItems that have been added to the UIList component during the render process.

我尝试覆盖组件上的BuildRenderTree方法以手动操作ChildContent RenderFragment,但是我唯一的选择是呈现或忽略片段(我无法窥视RenderFragment的内容代表).

I've attempted to override the BuildRenderTree method on my component to manually manipulate ChildContent RenderFragment but my only options are to render or ignore the fragment (I can't peek the content of the RenderFragment delegate).

    protected override void BuildRenderTree(RenderTreeBuilder builder)
    {
        int seq = 0;
        base.BuildRenderTree(builder);
        builder.OpenElement(seq++, "div");
        builder.AddContent(seq++, this.ChildContent);
        builder.CloseElement();
    }

我想知道是否可能出现以下情况.请注意,下面的语法无效,仅是举例说明这一点.

I'm wondering if something like below is possible. Please note that the syntax below is not valid, only an example to get the point across.

    protected int NumberOfItems{get;set;}

    protected override void BuildRenderTree(RenderTreeBuilder builder)
    {
        int seq = 0;
        base.BuildRenderTree(builder);
        builder.OpenElement(seq++, "div");
        foreach(var childFragment in this.ChildContent.GetFragments())
        {

           if(childFragment is UIListItem)
                NumberOfItems++;
           builder.AddContent(seq++,childFragment);
        }


        builder.CloseElement();
    }

推荐答案

这肯定是不可能的,至少在可预见的将来是不可能的. RenderFragment表示要渲染的一大块内容,据我所知,目前尚无法以编程方式访问您想要的内容.

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...

在Blazor中以及生活中,数据流都是从父组件到子组件.我猜这就是为什么必须根据这种固有"限制来设计他的软件的原因.原则上,我相信任何事情都是可以实现的,这取决于丹·罗斯(Dan Roth)所说的可用资源.

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.

关于模板化组件是什么?使用模板化组件,您的UIList组件知道并确定要呈现的UIListItem的数量...

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

顺便说一句,我想这是我可以在此答案中提供的唯一附加值:您没有正确使用序列.史蒂夫·安德森(Steve Anderson)为此撰写了一篇文章;搜索).您不应使用递增变量来发布序列.您应该改用数字,从0开始.

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...

这篇关于覆盖BuildRenderTree时是否可以获得RenderFragments列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆