如何在双向绑定asp.net core的blazor c#的3个嵌套组件之间进行通信(从父组件到其孙子组件,反之亦然).



我需要在后面的代码中blazor的3个组件之间进行通信。

  • 父组件有一个(提交按钮)按钮点击事件,在后面的代码中,我需要调用child-1组件通过传递一些参数并等待其响应返回中的bool值。
  • child-1组件通过调用带有确认按钮的对话框来与child-2通信
  • 当用户单击child-2组件中的确认按钮时,我需要将响应发送回child-1状态= true否则为false。
  • 然后child-1返回child-2给父进程的响应为status=true

注意: Child-1组件充当中介它没有任何UI元素

这就是这个概念,我不知道如何在parent后面的代码中调用child-1组件

例子:[In Parent Component]

<button @onclick="callChildOne">Submit<button/>
void callChildOne(){
// i need to call child-1 here and pass the parameter status
// i need help in writing this code
}

参考此图以更好地理解

我创立了这个案例,并从blazor的角度得出了答案。参考下面的例子

Child2.razor

@code{//some code}

Child1.razor

//No UI, only methods in code behind
@code{
public void Show()
{
// TODO actual implementation for calling child 2 component
}
}

Parent.razor

<button @onclick="callChildOne">Submit<button/>
@*Add a @ref to the component *@
<Child1 @ref="childObj"></Child1 >
@code {
//Hold the reference to the component
private Child1 childObj;
// Call the public methods of the component
private void callChildOne() => childObj.Show();    
}

这就是我如何在没有任何服务的情况下映射3个组件的关系。这个例子来自Blazor的博客