[英]Blazor: intercept an EventCallback to add some code
在我的 Blazor WebAssembly 应用程序中,我使用了很多由 Telerik Kendo 提供的 TelerikGrid 组件,但我的问题可能与另一个组件相同。 我需要在 TelerikGrid 的“OnRead”EventCallback 处理程序执行结束时执行一些代码,如下所示:
protected async Task OnReadHandler(GridReadEventArgs args)
{
args.Data = _myClient.GetData(); //some code
// This line is the one I need to repeat accros all handlers of OnRead in my application
await JSRuntime.InvokeVoidAsync("attachAllGridCells");
}
不知道Blazor能不能达到我的要求。 此外,无法修改组件 TelerikGrid,因为它来自第三方。 我试过一些东西,但没有好的结果:
public partial class CustomTelerikGrid<T> : TelerikGrid<T>
{
[Parameter]
public new EventCallback<GridReadEventArgs> OnRead { get; set; }
async Task TestAsync()
{
await this.OnRead.InvokeAsync(); // access to my "new" eventcallback
await base.OnRead.InvokeAsync(); // access to the "original" eventcallback
}
}
有什么办法可以实现吗? 我相信我对 Blazor 的了解并不是真的,但也许有人可以有一个想法,在此先感谢您的帮助。
我们的项目中有一个扩展TelerikGrid
的网格组件。 我们使用基础Telerik Grid
中的参数制作了 class TelerikGridSettings
,我们在整个项目的网格中使用这些参数,如下所示:
public partial class TelerikGridSettings<TItem> : BaseComponent
{
[Parameter]
public IEnumerable<TItem> Data { get; set; }
[Parameter]
public decimal RowHeight { get; set; }
[Parameter]
public RenderFragment GridColumns { get; set; }
[Parameter]
public EventCallback<GridReadEventArgs> OnRead { get; set; }
/// etc
}
然后在您的自定义GridComponent
中使用TelerikGrid
并像那样填充参数,并插入您的自定义OnReadHandler
:
<TelerikGrid TItem="TItem"
Data=@Data
RowHeight=@RowHeight
GridColumns=@GridColumns
OnRead=@OnReadHandler
// etc />
在您的OnReadHandler
,您将调用TelerikGrid
通用OnRead
事件回调,然后使用您的JS
方法:
private async ValueTask OnReadHandler()
{
await OnRead.InvokeAsync();
// your js method
}
然后,您就可以像现在使用GridComponent
一样使用TelerikGrid
。
希望这可以帮助。 此外,这种方法将使您在自定义网格时更加灵活。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.