从javascript示例调用monodroid方法

从javascript示例调用monodroid方法,mono,xamarin.android,Mono,Xamarin.android,我正在寻找一个如何使用webview从javascript调用monodroid方法(C#)的示例 比如: javascript: <a href="#" onclick="window.android.callAndroid('Hello from Browser')"> Call Android from JavaScript</a> 谢谢这是我用来为我最终解决这个问题的例子。迟做总比不做好 请密切注意函数的装饰,并确保在参考资料中包含Mono.Androi

我正在寻找一个如何使用webview从javascript调用monodroid方法(C#)的示例

比如:

javascript:

<a href="#" onclick="window.android.callAndroid('Hello from Browser')"> 
   Call Android from JavaScript</a>

谢谢

这是我用来为我最终解决这个问题的例子。迟做总比不做好

请密切注意函数的装饰,并确保在参考资料中包含Mono.Android.Export

使用系统;
使用Android.App;
使用Android.Content;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用Android.OS;
使用Android.Webkit;
使用Java.Interop;
命名空间WebViewJavaScriptInterface
{
[活动(Label=“Mono WebView ScriptInterface”,MainLauncher=true)]
公共类JavaScriptInterfaceActivity:活动
{
常量字符串html=@”
这是一段

点击我! "; 创建时受保护的覆盖无效(捆绑包) { base.OnCreate(bundle); //从“主”布局资源设置视图 SetContentView(Resource.Layout.Main); WebView=findviewbyd(Resource.Id.web); view.Settings.JavaScriptEnabled=true; view.SetWebChromeClient(新的WebChromeClient()); view.AddJavascriptInterface(新的Foo(this),“Foo”); view.LoadData(html,“text/html”,空); } } 类Foo:Java.Lang.Object { 公共Foo(上下文) { this.context=上下文; } public Foo(IntPtr handle、JNIHandle所有权转让) :底座(手柄、传输) { } 语境; [出口(“酒吧”)] //为了与Java/JS互操作约定保持一致,参数不能为System.String。 公共空栏(Java.Lang.String消息) { Console.WriteLine(“Foo.Bar已调用!”); Toast.MakeText(上下文,“这是来自C#!!”的祝酒词+消息,ToastLength.Short); } } }
public class LocalBrowser extends Activity {
...
   private class MyClass {
      public void callAndroid(final String arg) { 
               textView.setText(arg);
      }
   }
  }
using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

using Android.Webkit;
using Java.Interop;

namespace WebViewJavaScriptInterface
{
[Activity (Label = "Mono WebView ScriptInterface", MainLauncher = true)]
public class JavaScriptInterfaceActivity : Activity
{
    const string html = @"
<html>
<body>
<p>This is a paragraph.</p>
<button type=""button"" onClick=""Foo.bar('test message')"">Click Me!</button>
</body>
</html>";

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        WebView view = FindViewById<WebView> (Resource.Id.web);
        view.Settings.JavaScriptEnabled = true;
        view.SetWebChromeClient (new WebChromeClient ());
        view.AddJavascriptInterface (new Foo (this), "Foo");
        view.LoadData (html, "text/html", null);
    }
}

class Foo : Java.Lang.Object
{
    public Foo (Context context)
    {
        this.context = context;
    }

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

    Context context;

    [Export ("bar")]
    // to become consistent with Java/JS interop convention, the argument cannot be System.String.
    public void Bar (Java.Lang.String message)
    {
        Console.WriteLine ("Foo.Bar invoked!");
        Toast.MakeText (context, "This is a Toast from C#! " + message, ToastLength.Short).Show ();
    }
}

}