您的位置:首页 > 移动开发 > Android开发

Xamarin Android Webview中JS调用App中的C#方法

2018-10-25 15:51 706 查看

参考链接:https://github.com/xamarin/recipes/tree/master/Recipes/android/controls/webview/call_csharp_from_javascript

一、MainActivity中

//这个方法用于让H5调用android方法
web_view.AddJavascriptInterface(new JSXamarin(this), "JSXamarin");

二、定义一个接口类:

public class JSXamarin : Java.Lang.Object
{
Context context;

public JSXamarin(Context context)
{
this.context = context;
}

public JSXamarin(IntPtr handle, JniHandleOwnership transfer)
: base(handle, transfer)
{
}

[Export("ShowToast")]
[JavascriptInterface]
public void ShowToast(Java.Lang.String message)
{
Toast.MakeText(context, "Hello from C#"+message, ToastLength.Short).Show();
}

}

  

[Export("ShowToast")]
[JavascriptInterface]
16版本以上这两个属性不能少,需要引用一个Mono.Android.Export.dll

 

三、Web客户端调用:

function PrintSMDLab() {
JSXamarin.ShowToast(‘测试文字');
}

  


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: