简体   繁体   中英

EventCallback - is it a func delegate?

What does this mean?

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

Does this mean OnSelected is a delegate (function paramter) that holds input parameter of type Trail and return parameter void?

Why is EventCallback used?

If I have to return a parameter of type string for this delegate how would this declaration look like?

will it look like ?

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

To answer your first three questions:

An EventCallback is a readonly struct . It's a wrapper for a delegate that supports async behaviour through EventCallbackWorkItem .

It looks like this (extracted from the AspNetCore source code):

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;
}

You can see the above source code and other related code here - https://github.com/dotnet/aspnetcore/blob/main/src/Components/Components/src/EventCallback.cs

To answer your last two questions:

In your example Trail is what you return.

You would call an EventCallback that returns a string like this in the component:

<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);
    }
}

And consume it like this:

@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;
    }
}

If you want to return more complex data, create a struct or record to return.

For general usage see the MS-Docs article - https://docs.microsoft.com/en-us/aspnet/core/blazor/components/event-handling?view=aspnetcore-6.0#eventcallback .

EventCallback is a bound event handler delegate.

One of the most common scenarios for using EventCallback is to pass data from a child component to the parent component.

Here is a simple demo about how to pass the string value:

child component

<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());
    }
    
}

parent component

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;
    }
    
}

Demo

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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