Home > Enterprise >  How to pass IJSObjectReference to JS code as argument
How to pass IJSObjectReference to JS code as argument

Time:09-27

I've already read that I can invoke JS methods on IJSObjectReference but how to pass such object to JS code as an argument?

Let' say I have such JS code:

    addLayer: function (group,layer) {
      ...

In C# I have two references IJSObjectReference for both "group" and "layer", how do I pass them to such function?


Obtaining JS reference, example.

    createGroup: function () {
        var js_group = L.markerClusterGroup();
        return js_group;
    },

and C# call:

var js_ref = await jsRuntime.InvokeAsync<IJSObjectReference>($"{MarkerClusterInterop.BaseObjectContainer}.createGroup").ConfigureAwait(false);

CodePudding user response:

You can invoke addLayer function using IJSRuntime and pass the IJSObjectReferences as parameters after the function identifier.

@inject IJSRuntime JS

@code {
    private IJSObjectReference _groupJSObjectRef;
    private IJSObjectReference _layerJSObjectRef;

    ...

    private async Task SomeMethodAsync()
    {
        await JS.InvokeVoidAsync($"{MarkerClusterInterop.BaseObjectContainer}.addLayer", _groupJSObjectRef, _layerJSObjectRef);
    }
}
  • Related