简体   繁体   English

EventCallback - 它是一个 func 委托吗?

[英]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?这是否意味着 OnSelected 是一个委托(函数参数),它包含Trail类型的输入参数并返回参数 void?

Why is EventCallback used?为什么要使用 EventCallback?

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 . EventCallback 是一个readonly struct It's a wrapper for a delegate that supports async behaviour through EventCallbackWorkItem .它是通过EventCallbackWorkItem支持异步行为的委托的包装器。

It looks like this (extracted from the AspNetCore source code):它看起来像这样(从 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;
}

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您可以在此处查看上述源代码和其他相关代码 - 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.在您的示例中, Trail是您返回的内容。

You would call an EventCallback that returns a string like this in the component:您将调用一个 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);
    }
}

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

EventCallback is a bound event handler delegate. EventCallback是一个绑定的事件处理程序委托。

One of the most common scenarios for using EventCallback is to pass data from a child component to the parent component.使用EventCallback的最常见场景之一是将数据从子组件传递到父组件。

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演示

在此处输入图像描述

暂无
暂无

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

相关问题 事件回调字符串在 blazor 中不起作用 - Eventcallback string not working in blazor ASP.NET Core依赖项注入:服务在运行时使用Func委托解析 - ASP.NET Core dependency injection: Service resolved at runtime using a Func delegate 在 Blazor 中,如何使用 EventCallback 创建接口? - In Blazor, how can I create an interface with EventCallback? 如果 Changed 是 EventCallback 类型,为什么我们不能使用? 在 Changed?.InvokeAsync()? - If Changed is of type EventCallback, why can't we use ?. in Changed?.InvokeAsync()? .net core-将一个func列表与单个func组合在一起 - .net core - combine a list of func with or to a single func Blazor WASM 应用程序在处理执行删除请求的模态 ok 事件回调后停止响应 - Blazor WASM application stops responding after handling modal ok eventcallback, which performs a delete request Blazor:对EventCallback使用动态参数时,无效的参数类型匹配异常<T> - Blazor: Invalid argument type match exception when using dynamic for EventCallback<T> 无法从方法组转换为事件回调 blazor(服务器应用程序)-同步融合网格 - cannot convert from method group to eventcallback blazor (Server App) - Sync Fusion Grid 从 Blazor 组件 EventCallback 获取 ChangeEventArgs 值? 重构后目前为null - Getting the ChangeEventArgs value from Blazor component EventCallback? Currently is null after refactor 使用Func()参数进行ASP.NET Core Logging - ASP.NET Core Logging with Func() argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM