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

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

问题描述

对于 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的例子。 的Andr​​oid / MonoDroid的 Android的奇巧及以上使用Evaluatejavascript()?
具体怎么使用/安装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准确,但如果你能分享一些你的Andr​​oid C#code的,也许能帮助人们更好地

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
    }
}

然后就可以presumably说线沿线的东西。

Then you can presumably say something along the lines of

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

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

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