[英]Blazor component with custom EventCallback doesn't work
I'm creating a very simple Blazor component for my Blazor WebAssembly.我正在为我的 Blazor WebAssembly 创建一个非常简单的 Blazor 组件。 The component is a modal dialog.
该组件是一个模态对话框。 When the user click on a Cancel button the
EventCallBack
return false , true for the Ok .当用户点击取消按钮时,
EventCallBack
返回false , true为Ok 。 Here the code.这里是代码。
<div class="modal fade show" id="myModal"
style="display:block; background-color: rgba(10,10,10,.8);"
aria-modal="true" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">@Title</h4>
<button type="button" class="close"
@onclick="@ModalCancel">×</button>
</div>
<div class="modal-body">
<p>@Text</p>
</div>
<div class="modal-footer">
<button type="button" class="btn"
@onclick="@ModalCancel">Cancel</button>
<button type="button" class="btn btn-primary"
@onclick=@ModalOk>OK</button>
</div>
</div>
</div>
</div>
@code {
[Parameter] public long Id { get; set; }
[Parameter] public string Title { get; set; }
[Parameter] public string Text { get; set; }
[Parameter] public ModalDialogType DialogType { get; set; }
[Parameter] public EventCallback<bool> OnClose { get; set; }
private Task ModalCancel()
{
//return OnClose.InvokeAsync(new ModalDialogResponse()
//{
// Id = Id,
// IsOk = false
//});
return OnClose.InvokeAsync(false);
}
private Task ModalOk()
{
//return OnClose.InvokeAsync(new ModalDialogResponse()
//{
// Id = Id,
// IsOk = true
//});
return OnClose.InvokeAsync(true);
}
}
Now, when the main page receives the EventCallback
, what is the Id?现在,当主页收到
EventCallback
时,Id 是什么? Then, I thought to add a parameter for the Id and the callback returns a custom response然后,我想为Id添加一个参数,回调返回一个自定义响应
public class ModalDialogResponse
{
public long Id { get; set; }
public bool IsOk { get; set; }
}
If I declare in the component如果我在组件中声明
[Parameter] public EventCallback<ModalDialogResponse> OnClose { get; set; }
and then in the main page I call the modal dialog like然后在主页中我调用模态对话框
<ModalDialog Title="My title" Text="Here the message"
Id="@ItemId"
OnClose="@OnCloneDialogClose"
DialogType="ModalDialogType.YesNo" />
and then接着
private async Task OnCloneDialogClose(ModalDialogResponse response)
{
}
Visual Studio gives me an error Visual Studio 给我一个错误
CS1503 Argument 2: cannot convert from 'method group' to 'EventCallback'
CS1503 参数 2:无法从“方法组”转换为“事件回调”
I can't understand what it is wrong.我不明白这是怎么回事。
I have a recollection of Blazor not liking it when you change the signature of a callback (eg from bool to ModalDialogResponse).我记得 Blazor 在您更改回调的签名(例如从 bool 到 ModalDialogResponse)时不喜欢它。 I often see a similar error.
我经常看到类似的错误。 But, a full rebuild normally sorts it out.
但是,完全重建通常会解决它。
You could also try commenting out your <ModalDialog... />, rebuilding and then put it back and try again.您也可以尝试注释掉您的 <ModalDialog... />,重新构建,然后将其放回原处再试一次。
Or, deleting the obj and bin directories and rebuilding.或者,删除 obj 和 bin 目录并重建。
Finally, if you're looking for the Id, rather than pass it back up, as you already have it in the parent component, you could use:最后,如果您正在寻找 Id,而不是将其传回,因为您已经在父组件中拥有它,您可以使用:
<ModalDialog Title="My title" Text="Here the message"
Id="@ItemId"
OnClose=@((isOk) => OnCloneDialogClose(isOk, @ItemId))
DialogType="ModalDialogType.YesNo" />
private async Task OnCloneDialogClose(bool isOk, long id)
{
}
and keep the EventCall back declaration as:并将 EventCall 返回声明保持为:
[Parameter] public EventCallback<bool> OnClose { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.