Monodroid Javascript Call-back

后端 未结 6 1861
时光取名叫无心
时光取名叫无心 2020-12-30 11:53

I\'m trying to use monodroid with webkit to create an app. I am having a problem with letting the html page call a javascript method, which would be an interface to a method

6条回答
  •  醉梦人生
    2020-12-30 12:43

    // C#
    // !!!
    using Java.Interop; // add link to Mono.Android.Export
    
    public class Activity1 : Activity
    {
        const string html = @"
        
        
        

    This is a paragraph.

    "; class Foo : Java.Lang.Object // do not need Java.Lang.IRunnable { Context context; public Foo (Context context) { this.context = context; } [Export] // !!! do not work without Export [JavascriptInterface] // This is also needed in API 17+ public string SomeMethod(string param) { Toast.MakeText (context, "This is a Toast from C#!" + param, ToastLength.Short).Show (); } } protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); SetContentView (Resource.Layout.Main); WebView view = FindViewById (Resource.Id.web); view.Settings.JavaScriptEnabled = true; view.AddJavascriptInterface (new Foo (this), "Foo"); view.LoadData (html, "text/html", null); } }

提交回复
热议问题