参考链接: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
Android怎样调用系统类、
三、Web客户端调用:
function PrintSMDLab() {JSXamarin.ShowToast(‘测试文字');}