我们需要获取子组件列表OnAfterRenderAsync
父组件的方法,但是我们不知道该怎么做。
我们尝试与RenderBuilder
and GetFrames
但这始终为空。
我们有 Splitter 组件,其中 N 个分割区域作为子组件,我们需要获取父组件中的所有区域,在OnAfterRenderAsync
method.
如果您有一个 X 类型的父组件,并且想要引用 Y 类型的所有紧密耦合的子组件(例如 TabControl 和 TabPages),那么您可以执行此操作。
1:在 Parent 中添加一个包装器@ChildContent
添加指向自身的级联值。
<CascadingValue Value=@this>
@ChildContent
</CascadingValue>
2:在你的孩子中,你可以通过以下方式消耗该价值:CascadingParameter
@code
{
[CascadingParameter]
public YourParentComponent ParentComponent { get; set; }
}
3:您的孩子可以通知父母他们的存在
protected override void OnInitialized()
{
if (ParentComponent == null)
throw .............("Must be used within MyParentComponent");
MyParentComponent.AddChild(this);
}
如果您的子组件是有条件渲染的,那么让它们实现 IDisposable,以便它们可以通知父组件将它们从列表中删除。
Blazor University 上有一个示例,展示了如何创建 TabControl -https://blazor-university.com/templated-components-with-renderfragements/creating-a-tabcontrol/ https://blazor-university.com/templating-components-with-renderfragements/creating-a-tabcontrol/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)