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