繁体   English   中英

EventCallback - 它是一个 func 委托吗?

[英]EventCallback - is it a func delegate?

这是什么意思?

 public EventCallback<Trail> OnSelected { get; set; }

这是否意味着 OnSelected 是一个委托(函数参数),它包含Trail类型的输入参数并返回参数 void?

为什么要使用 EventCallback?

如果我必须为这个委托返回一个字符串类型的参数,这个声明会是什么样子?

它会是什么样子?

public EventCallback<Trail, string> OnSelected { get; set; }

要回答您的前三个问题:

EventCallback 是一个readonly struct 它是通过EventCallbackWorkItem支持异步行为的委托的包装器。

它看起来像这样(从 AspNetCore 源代码中提取):

public readonly struct EventCallback<TValue> : IEventCallback
{
    public static readonly EventCallback<TValue> Empty = new EventCallback<TValue>(null, (Action)(() => { }));

    internal readonly MulticastDelegate? Delegate;
    internal readonly IHandleEvent? Receiver;

    public EventCallback(IHandleEvent? receiver, MulticastDelegate? @delegate)
    {
        Receiver = receiver;
        Delegate = @delegate;
    }

    public bool HasDelegate => Delegate != null;

    internal bool RequiresExplicitReceiver 
        => Receiver != null && !object.ReferenceEquals(Receiver, Delegate?.Target);

    public Task InvokeAsync(TValue? arg)
    {
        if (Receiver == null)
            return EventCallbackWorkItem.InvokeAsync<TValue?>(Delegate, arg);

        return Receiver.HandleEventAsync(new EventCallbackWorkItem(Delegate), arg);
    }

    public Task InvokeAsync() => InvokeAsync(default!);

    internal EventCallback AsUntyped()
        => new EventCallback(Receiver ?? Delegate?.Target as IHandleEvent, Delegate);

    object? IEventCallback.UnpackForRenderTree()
        => return RequiresExplicitReceiver ? (object)AsUntyped() : Delegate;
}

您可以在此处查看上述源代码和其他相关代码 - https://github.com/dotnet/aspnetcore/blob/main/src/Components/Components/src/EventCallback.cs

要回答您的最后两个问题:

在您的示例中, Trail是您返回的内容。

您将调用一个 EventCallback,它在组件中返回一个类似这样的字符串:

<div class="row">
    <div class="col-auto">
        <input class="form-control" type="text" @bind="@this.enteredValue" />
    </div>
    <div class="col-auto">
        <button class="btn btn-primary" @onclick=this.HandleSelect>Set Me</button>
    </div>
    <div class="col-auto">
    <button class="btn btn-secondary" @onclick=this.SetSelect>Set Me To Hello</button>
    </div>
</div>

<div class="p-2 m-2 bg-dark text-white">
    Value: @this.Value
</div>
@code {
    private string enteredValue = string.Empty;
    [Parameter] public EventCallback<string> OnSelected { get; set; }

    [Parameter, EditorRequired] public string Value { get; set; } = string.Empty;

    private async Task SetSelect()
    {
        await OnSelected.InvokeAsync("Hello");
    }

    private async Task HandleSelect()
    {
        await OnSelected.InvokeAsync(enteredValue);
    }
}

并像这样消费它:

@page "/"
<h2>Test Page</h2>
<MyComponent Value=@this.textValue OnSelected=this.HandleValueChanged />

@code {
    private string textValue = string.Empty;

    private async Task HandleValueChanged(string value)
    {
        // Emulate some async activity like getting data
        await Task.Delay(1000);
        this.textValue = value;
    }
}

如果要返回更复杂的数据,请创建要返回的结构或记录。

有关一般用法,请参阅 MS-Docs 文章 - https://docs.microsoft.com/en-us/aspnet/core/blazor/components/event-handling?view=aspnetcore-6.0#eventcallback

EventCallback是一个绑定的事件处理程序委托。

使用EventCallback的最常见场景之一是将数据从子组件传递到父组件。

这是一个关于如何传递字符串值的简单演示:

子组件

<h3>TestChild</h3>

<input @onchange="UseEcb"/>

@code {
    [Parameter]
    public EventCallback<string> RecoverRequest { get; set; }
        
    async Task UseEcb(ChangeEventArgs e)
    {
        await RecoverRequest.InvokeAsync(e.Value.ToString());
    }
    
}

父组件

page "/counter"

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<h2>@result</h2>

<TestChild RecoverRequest="Test"></TestChild>

@code {
    

    [Parameter]
    public string result { get; set; }

    private void Test(string a)
    {
        result = "Child Component value is "+a;
    }
    
}

演示

在此处输入图像描述

暂无
暂无

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

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