[英]Blazor: intercept an EventCallback to add some code
In my Blazor WebAssembly application, I use a lot the TelerikGrid component, provided by Telerik Kendo, but my question could be the same with another component.在我的 Blazor WebAssembly 应用程序中,我使用了很多由 Telerik Kendo 提供的 TelerikGrid 组件,但我的问题可能与另一个组件相同。 I need to execute some code at the end of the execution of the handler of the "OnRead" EventCallback of TelerikGrid, as bellow:
我需要在 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");
}
I do not know if Blazor can achieve my request.不知道Blazor能不能达到我的要求。 In addition, the component TelerikGrid cannot be modified, as it is from a third-party.
此外,无法修改组件 TelerikGrid,因为它来自第三方。 I've tried something, with no good result:
我试过一些东西,但没有好的结果:
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
}
}
Is there way I can achieve that?有什么办法可以实现吗? I believe not really with my knowledge of Blazor, but maybe someone can have an idea, thanks in advance for any help.
我相信我对 Blazor 的了解并不是真的,但也许有人可以有一个想法,在此先感谢您的帮助。
We have a grid component in our project that extends TelerikGrid
.我们的项目中有一个扩展
TelerikGrid
的网格组件。 We made class TelerikGridSettings
with parameters from base Telerik Grid
which we use in our grids across our project like that:我们使用基础
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
}
Then in your custom GridComponent
you use TelerikGrid
and fill parameters like that, and also insert your custom OnReadHandler
:然后在您的自定义
GridComponent
中使用TelerikGrid
并像那样填充参数,并插入您的自定义OnReadHandler
:
<TelerikGrid TItem="TItem"
Data=@Data
RowHeight=@RowHeight
GridColumns=@GridColumns
OnRead=@OnReadHandler
// etc />
In your OnReadHandler
you will invoke TelerikGrid
common OnRead
event callback and after that use your JS
method:在您的
OnReadHandler
,您将调用TelerikGrid
通用OnRead
事件回调,然后使用您的JS
方法:
private async ValueTask OnReadHandler()
{
await OnRead.InvokeAsync();
// your js method
}
Then you can use your GridComponent
exactly as you use TelerikGrid
right now.然后,您就可以像现在使用
GridComponent
一样使用TelerikGrid
。
Hope this helps.希望这可以帮助。 Also this approach will give you more flexibility in customizing your grid.
此外,这种方法将使您在自定义网格时更加灵活。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.