如何使用 MonoTouch 调用 javascript 函数,反之亦然? [英] How I can call a javascript function with MonoTouch and vice versa?
问题描述
我是使用单点触控的新手.
i'm new using monotouch.
我的问题是我想从 monotouch 函数调用 javascript 函数,javascript 函数也可以调用 monotouch 函数.
My problem is i want call a javascript function from a monotouch function and javascript function could call a monotouch function too.
我相信使用客观 c 这个问题是可能的,但我需要用单点触控来解决.
I believe that with objective c this problem is possible, but i need making it with monotouch.
请帮帮我.
感谢您的帮助
推荐答案
要从应用程序调用 UIWebView 中运行的 Javascript 代码,请使用 EvaluateJavascript 方法,如下所示:
To invoke Javascript code running in the UIWebView from your application, use the EvaluateJavascript method, like this:
myView.EvaluateJavaScript ("a = 1;");
要回调到您的 C# 代码中,唯一的选择是像这样连接到 ShouldStartLoad 属性:
To call back into your C# code, the only option is to hook up to the ShouldStartLoad property like this:
myView.ShouldStartLoad = myHandler;
[...]
bool myHandler (UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navType)
{
// Determine what to do here based on the @request and @navType
}
当然,如果您想轻松访问局部变量,也可以使用匿名方法:
You can of course, also use anonymous methods if you want to access local variables easily:
myView.ShouldStartLoad = (webView, request, navType) => {
// Determine here what to do
}
在 Javascript 端,如果你想回调 Mono,那么你可以将 location.href 属性设置为一个特殊"的 url,如下所示:
In the Javascript side, if you want to call back to Mono, you then set the location.href property to a "special" url, like this:
// Javascript code:
location.href = "myapp://action?par1=abc&par2=def"
信息将在请求对象上可用:request.Url.AbsoluteString
The information will be available on the request object: request.Url.AbsoluteString
这篇关于如何使用 MonoTouch 调用 javascript 函数,反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!