Android Xamarin 中的 WebView.EvaluateJavaScript 问题 [英] Issue with WebView.EvaluateJavaScript in Android Xamarin

查看:43
本文介绍了Android Xamarin 中的 WebView.EvaluateJavaScript 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码将 Java 脚本注入到我的 Android Web 视图中

I am using the following code for injecting Java Script in to my Android Web view

WebView

webView = FindViewById<WebView> (Resource.Id.learningWebView);
if (null != webView) {
    webView.Settings.JavaScriptEnabled = true;
    webView.Settings.SetSupportZoom (true);
    webView.SetWebViewClient (new CustomWebViewClient ());

 }

WebView 客户端实现

public class CustomWebViewClient : WebViewClient
{
    public override bool ShouldOverrideUrlLoading (WebView view, string url)
    {
        view.LoadUrl (url);
        return true;
    }

    public override void OnPageStarted (WebView view, string url, Android.Graphics.Bitmap favicon)
    {

    }

    public override void OnPageFinished (WebView view, string url)
    {
        base.OnPageFinished (view, url);
        HideLearningDivs (view);
    }


    void HideLearningDivs (WebView view)
    {
        try {

            view.EvaluateJavascript ("document.getElementById(\"suiteBar\").parentNode.style.display='none'", new JavaScriptResult ());
        } catch (Exception ex) {
            Console.WriteLine (ex.Message);
        }

    }

IValueCallback 实现

public class JavaScriptResult :  IValueCallback
{
    public IntPtr Handle {
        get;
        set;
    }

    public void Dispose ()
    {

    }



    public void OnReceiveValue (Java.Lang.Object result)
    {

    }
}

但在执行应用程序期间,我收到以下错误.

But during the time of executing the application I am getting the following error.

java.lang.NoSuchMethodError: 在类 Landroid/webkit/WebView 中没有带有 name='evaluateJavascript' signature='(Ljava/lang/String;Landroid/webkit/ValueCallback;)V' 的方法;

谁能帮我找出我的实现有什么问题.

Can anyone please help me to find what is wrong with my implementation.

推荐答案

我将链接到我在下面找到答案的位置,但基本上您需要检查 Android KitKat (4.4),因为该功能直到然后.如果设备运行的版本低于 4.4,那么您可能需要做一些不同的事情来获取值,如果您确实需要对其进行处理.例如使用某种Hybrid WebView(查看Xamarin Forms Labs 版本可能)和/或使用 AddJavaScriptInterface()

I will link to where I found the answer below, but basically you need to do a check for Android KitKat (4.4), since that function was not introduced until then. If the device is running lower than 4.4, then you may need to do something different to get the value back if you actually need to do something with it. Such as using a Hybrid WebView of some kind (check out Xamarin Forms Labs version of it perhaps) and/or using the AddJavaScriptInterface()

代码如下:

if(Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat) {
    webView.EvaluateJavascript("javascript:GoBack();", null);
} else {
    webView.LoadUrl("javascript:GoBack();");
}

https://forums.xamarin.com/discussion/24894/webview-evaluatejavascript-issues

*自从写这篇文章以来,我发现了一个 优秀的帖子 Adam Pedley(我显然最近一直链接到它)其中涵盖了为 Xamarin Forms 执行此操作的内容,但也提到了 Android 4.2 对 JS 引擎的更改.第一次运行 JavaScript 可能会起作用,但它也会将文档对象设置为脚本结果,因此您可能需要将 document.getElementById() 的结果分配给变量以解决此问题: var x = document.getElementById().

* Since writing this, I found an excellent post Adam Pedley (who I apparently have been linking to a lot lately) which covers doing this for Xamarin Forms but also mentions a change in Android 4.2 to the JS engine. Running the JavaScript might work the first time but it also sets the document object to the script result, so you may need to assign the result of document.getElementById() to a variable in order to work around this: var x = document.getElementById().

这篇关于Android Xamarin 中的 WebView.EvaluateJavaScript 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆