简体   繁体   English

如何知道 EventCallback 何时被触发?

[英]How to know when EventCallback has been fired?

I'm doing some testing with razor components but I have an issue to update a property from the child component to the grand-parent component.我正在对 razor 组件进行一些测试,但是我遇到了将属性从子组件更新到祖父组件的问题。

I'm using EventCallback to update my parent component when the child component updates a property.当子组件更新属性时,我使用 EventCallback 来更新我的父组件。 It works well for architecture with two levels (ParentComponent/ChildComponent) however, it doesn't work with three levels (GrandParentComponent/ParentComponent/ChildComponent).它适用于具有两个级别(ParentComponent/ChildComponent)的架构,但是,它不适用于三个级别(GrandParentComponent/ParentComponent/ChildComponent)。

Let's take an example with three components A, B and C.让我们以三个组件 A、B 和 C 为例。

- A (GrandParentComponent)
-- B (ParentComponent)
--- C (ChildComponent)
  • Updating B will fire EventCallback to update A更新 B 将触发 EventCallback 来更新 A
  • Updating C will fire EventCallback to update B, however at this stage B doesn't trigger EventCallback once updated so the A component is still not updated.更新 C 将触发 EventCallback 以更新 B,但是在此阶段 B 更新后不会触发 EventCallback,因此 A 组件仍未更新。

How can you know if a component has been updated by an EventCallback.如何知道组件是否已被 EventCallback 更新。

I would like to know that so I can trigger EventCallback from B when EventCallback from C has been fired.我想知道这一点,所以当来自 C 的 EventCallback 被触发时,我可以从 B 触发 EventCallback。 Does it make sense?是否有意义? :D :D

How to know when EventCallback has been fired?如何知道 EventCallback 何时被触发?

Define a an event delegate that you can trigger when the EventCallback is fired... just jocking.定义一个事件委托,您可以在触发 EventCallback 时触发该委托……只是在开玩笑。

You can do that in various ways.您可以通过各种方式做到这一点。 Here's one:这是一个:

ComponentA.razor ComponentA.razor

<ComponentB ComponentBEvent="EventCallback.Factory.Create<string>(this, 
          mymethod)"></ComponentB>
<p>Message from Component A @message</p>

@code {
    private string message;

    private Task mymethod(string str)
   {
       message = str;
       return  Task.CompletedTask;
   }
}

ComponentB.razor ComponentB.razor

<ComponentC ComponentCEvent="EventCallback.Factory.Create<string>(this, 
                                                     mymethod)"></ComponentC>

<p>Message from Component B @message</p>

@code {
    string myvalue;
    [Parameter]
    public EventCallback<string> ComponentBEvent { get; set; }

    private string message;

    private async Task mymethod(string str)
    {
         message = str;

        if(ComponentBEvent.HasDelegate)
        {
           await ComponentBEvent.InvokeAsync(str);
        }
    }
 }

ComponentC.razor ComponentC.razor

<input type="text" value="@myvalue" @oninput="@((args) => { myvalue = 
   args.Value.ToString(); ComponentCEvent.InvokeAsync(args.Value.ToString()); 
})" />


<p>Message from Component C @myvalue</p>

@code {
     string myvalue;
     [Parameter]
     public EventCallback<string> ComponentCEvent { get; set; }
}

Usage用法

<ComponentA />

Note: You may implement this behavior using a notifier service, which employ the state pattern.注意:您可以使用使用状态模式的通知程序服务来实现此行为。 This service control the state of objects, update, delete, etc., and define events that are fired when an action occurs, say, an employee object was added in component A, in which case the notifier service notifies all interested parties (subscribing components) of this fact.该服务控制对象的状态、更新、删除等,并定义发生动作时触发的事件,例如,在组件 A 中添加了一个员工对象,在这种情况下,通知服务通知所有相关方(订阅组件) ) 这个事实。

Hope this helps...希望这可以帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM