jsakamoto / Toolbelt.Blazor.HotKeys2

This is a class library that provides configuration-centric keyboard shortcuts for your Blazor apps.
https://jsakamoto.github.io/Toolbelt.Blazor.HotKeys2/
Mozilla Public License 2.0
87 stars 6 forks source link

Issue with calling async Task to process hotkey #8

Closed grumpykiwi closed 1 year ago

grumpykiwi commented 1 year ago

I have upgraded to the new version of nuget package at Visual Studio recommendation and now have a bit of an issue with the code

Does this library support Blazor Server now ?

My callback function to handle the hotkeys needs to call an async function in order to display a modal dialog. How do I modify the call to CreateContext to avoid a compiler error. Here is the code:

this.HotKeysContext = this.HotKeys.CreateContext()
        .Add(ModCode.Ctrl|ModCode.Shift, Code.W, ProcessHotKey, "Process HotKey");

I get "Argument 3 cannot convert from method group to string

Here is the method:

    protected async Task ProcessHotKey()
    {
        await ShowSearchModal();
    }
jsakamoto commented 1 year ago

@grumpykiwi I recommend to use ValueTask instead of Task, like the following code.

protected async ValueTask ProcessHotKey()
{
    await ShowSearchModal();
}

If you have to use Task, you can do that with the following code.

this.HotKeysContext = this.HotKeys.CreateContext()
    .Add(ModCode.Ctrl|ModCode.Shift, Code.W, async () => await ProcessHotKey(), "Process HotKey");