[英]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.