Call C# method with parameter from JS xamarin

476 Views Asked by At

I tried to call C# method from JS with arguments, but I've got an error.

I'm using Xamarin Android (not Xamarin.Forms)

C# Code:

[JavascriptInterface]
[Export("test")]
public Java.Lang.String Test(Java.Lang.String hello)
{
    return hello;
}

JS Code:

var foo = GameBridge.test('foo');

Error:System.InvalidOperationException: Specified managed method 'Test' was not found. Signature: (Ljava/lang/String;)Ljava/lang/String;

Error screenshot

1

There are 1 best solutions below

0
sun wells On

The problem is the return type of the c# method. It works well with return type as 'void'. Below code working to me.

[JavascriptInterface]
[Export("test")]
public void Test(string hello)
{
    //to do work
}

I am also looking for to handle return types in Export/JavascriptInterface.