EdCharbeneau / BlazorProSnippets

A collection of Blazor snippets for .NET ninjas.
Other
23 stars 4 forks source link

Snippet for JsRuntime.InvokeAsync #4

Closed EdCharbeneau closed 4 years ago

EdCharbeneau commented 5 years ago

Requested by Sillyporridge on show

JsRuntime.InvokeAsync

EdCharbeneau commented 5 years ago

Suggested snippets

public async ValueTask<$bool$> $DotnetMethod$()
{
    return await _jsRuntime.InvokeAsync<$bool$>("$jsMethod$", DotNetObjectReference.Create(this), $args$);
}

[JSInvokable]
public void $CallbackMethod$() {
    // some callback method
    // use the interopc snippet in a JavaScript file
    // to complete the JavaScript method
    $end$
}
window.$jsNameSpace$ = {
    $jsMethodName$: function (dotnetHelper, args) {

        console.log("js method invoked");

        dotnetHelper.invokeMethodAsync('$CallbackMethod$', callbackArgs);

        console.log("dotnet method inovoked");
        // use the interopc snippet in a C# class file
        // to complete the JavaScript method

        return true;
    }
};
EdCharbeneau commented 4 years ago

Added in 2.1