需要一些帮助。Razor 组件的新手。我有一个托管发票的 Razor 页面,其中的一个子部分是付款。我创建了一个 Razor 组件来处理付款并更新名为 CurrentModel 的属性。我想返回对实例化 Razor 组件的 Razor 页面所做的更改。我想我可以通过将组件参数设置为方法来做到这一点(基于https://learn.microsoft.com/en-us/aspnet/core/blazor/components/event-handling?view=aspnetcore-9.0#eventcallback)。
成分:
[Parameter]
public EventCallback<List<Partial>> TestCallBack { get; set; }
然后稍后在组件中:
private async Task SetAmount(ChangeEventArgs e, int paymentId)
{
var amount = e.Value.ToDecimal();
if (amount.HasValue)
{
CurrentModel = CurrentModel.SetAmount(paymentId, amount.Value, Validator);
}
await TestCallBack.InvokeAsync(CurrentModel);
}
Razor Page 中的组件声明:
<component type="typeof(Components.Invoices.Payments)" render-mode="ServerPrerendered" param-testcallback="@Model.ReturnCallBack" />
(也尝试过)
<component type="typeof(Components.Invoices.Payments)" render-mode="ServerPrerendered" TestCallBack="@Model.ReturnCallBack" />
public async Task ReturnCallBack(List<Partial> partials)
{
...
}
无论哪种方式,VS 都会告诉我:
将方法组“ReturnCallback”转换为非委托类型对象。您是否打算调用该方法?
答案当然是肯定的,我确实希望调用该方法,但我不知道如何让该方法一直有效。上面的内容似乎使委托无效。
我的方法不是标准方法吗?我是否应该遵循其他方法?
我期望这种方法能够奏效,但是有些事情阻碍了委派。