如何在 Xamarin.Android/Monodroid 中使用 Android KitKat EvaluateJavascript 并检索结果? [英] How to use Android KitKat EvaluateJavascript in Xamarin.Android/Monodroid and retrieve result?

查看:20
本文介绍了如何在 Xamarin.Android/Monodroid 中使用 Android KitKat EvaluateJavascript 并检索结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 Xamarin.iOS/Monotouch,在评估 javascript 时检索字符串很简单.

For Xamarin.iOS/Monotouch it is simple to retrieve a string when evaluating javascript.

例如

string elementValue = browser.EvaluateJavascript("document.getElementById('Id').value");

if(elementValue != "")
{
   DoSomething();
}

谁能提供一个如何为 Xamarin 执行此操作的示例.Android/Monodroid 使用 Evaluatejavascript() 的 Android kitkat 及更高版本?具体如何使用/设置 IValueCallback 同步.

Could anybody provide an example of how to do this for Xamarin. Android/Monodroid Android kitkat and above using Evaluatejavascript()? Specifically how to use/setup IValueCallback Synchronously.

推荐答案

Android 的 WebView 异步执行 JavaScript,稍后在 ValueCallback 中传递执行结果.

Android's WebView executes JavaScript asynchronously and passes the result of the execution in a ValueCallback later.

我对 Xamarin 不是很熟悉,但如果你能分享一些你的 Android C# 代码,也许能更好地提供帮助.

I'm not familiar with Xamarin exactly, but if you can share some of your Android C# code, may be able to help better.

从 Java 的角度,我的经验所在:

From a Java perspective, where my experience lies:

本质上,您需要实例化一个实现 onReceiveValue 函数的 ValueCallback 对象.当您评估的 javascript 完成执行时,您声明的函数将被执行,将该评估的结果作为 JSON 字符串的参数传递.然后,您应该能够从该 JSON 中检索结果.

Essentially, you need to instantiate a ValueCallback object that implements the onReceiveValue function. When the javascript that you have evaluated finishes executing, the function you declared will be executed, passing in the result of that evaluation as a paramter that is string of JSON. You should then be able to retrieve the result from that JSON.

希望这有帮助!

更新:拼凑一些可能有帮助的 C#.警告:在此之前我从未写过一行 c#.但希望如果它不太正确会让你走上正确的道路:-)

Update: cobbling together some C# that might help. Caveat: I've never written a line of c# before this. But hopefully if it's not quite right will set you on the right path :-)

class JavaScriptResult : public IValueCallback {
    public void OnReceiveValue(Java.Lang.Object result) {
        Java.Lang.String json = (Java.Lang.String) result;
        // |json| is a string of JSON containing the result of your evaluation
    }
}

然后你大概可以说一些类似的东西

Then you can presumably say something along the lines of

web_view.EvaluateJavaScript("function() { alert('hello world'); return 42; }()",
        new JavaScriptResult());

这篇关于如何在 Xamarin.Android/Monodroid 中使用 Android KitKat EvaluateJavascript 并检索结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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