serdarciplak / BlazorMonaco

Blazor component for Microsoft's Monaco Editor which powers Visual Studio Code.
https://serdarciplak.github.io/BlazorMonaco/
MIT License
432 stars 98 forks source link

onkeydown and onkeyup invalid cast #125

Closed DJSures closed 4 months ago

DJSures commented 4 months ago

Great component, thanks for creating the JS invokes for us!

This issue should be a simple one - I haven't looked at your code to see where to change it. But I'm sure you can whip up a bug fix quickly. The onkeydown and onkeyup have invalid casts between the editor function (KeyboardEvent) and parameter type (KeyboardEventArgs).

Because of this we get a casting error...

     Unhandled exception rendering component: Unable to set property 'onkeydown' on object of type 'BlazorMonaco.Editor.StandaloneCodeEditor'. 

The error was: Unable to cast object of type 'Microsoft.AspNetCore.Components.EventCallback`1[Microsoft.AspNetCore.Components.Web.KeyboardEventArgs]' to type 'Microsoft.AspNetCore.Components.EventCallback`1[BlazorMonaco.KeyboardEvent]'.

      System.InvalidOperationException: Unable to set property 'onkeydown' on object of type 'BlazorMonaco.Editor.StandaloneCodeEditor'. The error was: Unable to cast object of type 'Microsoft.AspNetCore.Components.EventCallback`1[Microsoft.AspNetCore.Components.Web.KeyboardEventArgs]' to type 'Microsoft.AspNetCore.Components.EventCallback`1[BlazorMonaco.KeyboardEvent]'.
       ---> System.InvalidCastException: Unable to cast object of type 'Microsoft.AspNetCore.Components.EventCallback`1[Microsoft.AspNetCore.Components.Web.KeyboardEventArgs]' to type 'Microsoft.AspNetCore.Components.EventCallback`1[BlazorMonaco.KeyboardEvent]'.
         at Microsoft.AspNetCore.Components.Reflection.PropertySetter.CallPropertySetter[TTarget,TValue](Action`2 setter, Object target, Object value)
         at Microsoft.AspNetCore.Components.Reflection.ComponentProperties.<SetProperties>g__SetProperty|3_0(Object target, PropertySetter writer, String parameterName, Object value)
         --- End of inner exception stack trace ---
         at Microsoft.AspNetCore.Components.Reflection.ComponentProperties.<SetProperties>g__SetProperty|3_0(Object target, PropertySetter writer, String parameterName, Object value)
         at Microsoft.AspNetCore.Components.Reflection.ComponentProperties.SetProperties(ParameterView& parameters, Object target)
         at Microsoft.AspNetCore.Components.ComponentBase.SetParametersAsync(ParameterView parameters)
         at Microsoft.AspNetCore.Components.Rendering.ComponentState.SupplyCombinedParameters(ParameterView directAndCascadingParameters)

I need to know when a key is pressed to for use in my application. Thanks again!

DJSures commented 4 months ago

Oh, I see - you've created your own parameters to not use the default @onkey____ parameters.

So for anyone else in the future, while you're used to using @onkeyup, etc... Instead just use the OnKeyUp and OnKeyDown parameters that the author created. A bit different than usual use of existing parameters.

    <BlazorMonaco.Editor.StandaloneCodeEditor @ref="_tbCodeEditor"
                                              OnKeyUp="onKeyUp" />
    [JSInvokable]
    void onKeyUp(BlazorMonaco.KeyboardEvent e) {

      Console.WriteLine(e.KeyCode);
   }