Monodroid Javascript Call-back

后端 未结 6 1902
时光取名叫无心
时光取名叫无心 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:35

    I found that calling LoadData after AddJavascriptInterface was necessary. Also, with this change, assigning a WebChromeClient was not needed. For example, the modified version below runs fine:

    public class Activity1 : Activity
    {
        const string html = @"
        
        
        

    This is a paragraph.

    "; 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); } class Foo : Java.Lang.Object, Java.Lang.IRunnable { Context context; public Foo (Context context) { this.context = context; } public void Run () { Toast.MakeText (context, "This is a Toast from C#!", ToastLength.Short).Show (); } } }

提交回复
热议问题