使用JS xamarin中的参数调用C#方法

时间:2022-08-26 11:23:59

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

我试图用JS从参数中调用C#方法,但是我遇到了错误。

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

我正在使用Xamarin Android(不是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;

错误:System.InvalidOperationException:找不到指定的托管方法“Test”。签名:(Ljava / lang / String;)Ljava / lang / String;

Error screenshot

1 个解决方案

#1


0  

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

问题是c#方法的返回类型。它适用于返回类型为'void'。下面的代码对我有用。

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

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

我也在寻找处理Export / JavascriptInterface中的返回类型。

#1


0  

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

问题是c#方法的返回类型。它适用于返回类型为'void'。下面的代码对我有用。

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

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

我也在寻找处理Export / JavascriptInterface中的返回类型。

相关文章