diff --git a/src/Masa.Blazor/Components/Button/MButton.razor.cs b/src/Masa.Blazor/Components/Button/MButton.razor.cs index d39d03201f..76010715db 100644 --- a/src/Masa.Blazor/Components/Button/MButton.razor.cs +++ b/src/Masa.Blazor/Components/Button/MButton.razor.cs @@ -240,10 +240,7 @@ protected async Task HandleOnClick(MouseEventArgs args) await Js.InvokeVoidAsync(JsInteropConstants.Blur, Ref); } - if (OnClick.HasDelegate) - { - await OnClick.InvokeAsync(args); - } + await InvokeCallbackAsync(OnClick, args); await ToggleAsync(); } diff --git a/src/Masa.Blazor/Components/Card/MCard.razor.cs b/src/Masa.Blazor/Components/Card/MCard.razor.cs index b803893be2..5a867cc308 100644 --- a/src/Masa.Blazor/Components/Card/MCard.razor.cs +++ b/src/Masa.Blazor/Components/Card/MCard.razor.cs @@ -92,7 +92,7 @@ protected override void OnParametersSet() (Tag, Attributes) = _router.GenerateRouteLink(); Attributes["ripple"] = Ripple && IsClickable; - Attributes["onclick"] = OnClick; + Attributes["onclick"] = EventCallback.Factory.Create(this, args => InvokeCallbackAsync(OnClick, args)); } private static Block _block = new("m-card"); diff --git a/src/Masa.Blazor/Core/MasaComponentBase.cs b/src/Masa.Blazor/Core/MasaComponentBase.cs index fc4176f261..3d5d8f33cd 100644 --- a/src/Masa.Blazor/Core/MasaComponentBase.cs +++ b/src/Masa.Blazor/Core/MasaComponentBase.cs @@ -420,4 +420,31 @@ private async Task CallStateHasChangedOnAsyncCompletion(Task task) } #endregion + + /// + /// Invoke a callback event and use the error handler to catch exceptions + /// when an error occurs. + /// + /// + /// + /// + protected async Task InvokeCallbackAsync(EventCallback callback, TEventArgs args) + { + try + { + if (callback.HasDelegate) + { + await callback.InvokeAsync(args); + } + } + catch (Exception e) + { + if (ErrorHandler is null) + { + throw; + } + + await ErrorHandler.HandleExceptionAsync(e); + } + } } \ No newline at end of file