InvokeScript()方法返回null。
JavaScript的:
function gpsToAddress(gpsX, gpsY) {
var coords = new daum.maps.LatLng(gpsX, gpsY);
geocoder.coord2detailaddr(coords, CallbackA)}
function CallbackA(status, result) {
if(status===daum.maps.services.Status.OK)
{
return result[0].jibunaddress;
}
}
和C#:
private void f_ret_gpstoaddress(double v_gps_x, double v_gps_y,out string v_address)
{
object[] args = { "gpsToAddress(" + v_gps_x + "," + v_gps_y + ");" };
v_address = (string)webBrowser1.Document.InvokeScript("eval", args);
return;
}
private void button3_Click(object sender, EventArgs e)
{
f_ret_gpstoaddress(37.353933, 127.944739, out v_address);
MessageBox.Show(v_address);
}
' args'和' v_address'返回null,messageBox也返回null。 我想要返回一些值。 拜托,帮助我!
编辑:好的,我编辑了这样的C#代码:private string f_ret_gpstoaddress(double v_gps_x, double v_gps_y, out string v_address)
{
var args = "gpsToAddress(" + v_gps_x + "," + v_gps_y + ");" ;
v_address = webBrowser1.Document.InvokeScript("eval",new object[] { args }).ToString();
return v_address;
}
private void button3_Click(object sender, EventArgs e)
{
f_ret_gpstoaddress(37.353933, 127.944739, out v_address);
MessageBox.Show(v_address);
}
因此,args不为null,但v_address仍为null。什么问题??
答案 0 :(得分:0)
您的函数gpsToAddress不返回任何内容。但是当进程完成时它调用了一个回调函数。
我建议您在调用函数时传递回调函数。
请查看此主题以获取更多详细信息:Make async event synchronous in JavaScript
编辑:基于以上问题:
创建一个回调类
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class Callback
{
// allows an instance of Callback to look like a function to the script
// (allows callback() rather than forcing the script to do callback.callMe)
[System.Runtime.InteropServices.DispId(0)]
public void callMe(string v_address)
{
MessageBox.Show(v_address);
}
}
使用回调调用js函数:
private string f_ret_gpstoaddress(double v_gps_x, double v_gps_y, out string v_address)
{
Callback cb = new Callback();
var args = "gpsToAddress(" + v_gps_x + "," + v_gps_y + "," + cb);" ;
v_address = webBrowser1.Document.InvokeScript("eval",new object[] { args }).ToString();
return v_address;
}
js函数将回调作为参数:
function gpsToAddress(gpsX, gpsY, callback) {
function CallbackA(status, result) {
if(status===daum.maps.services.Status.OK)
{
callback(result[0].jibunaddress);
}
else {
callback('');
}
}
var coords = new daum.maps.LatLng(gpsX, gpsY);
geocoder.coord2detailaddr(coords, CallbackA)
}
编辑2 :(虚拟样本)
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string javascript = @"<html><head><script type='text/javascript'>function gpsToAddress(param1, callback) {
function CallbackA()
{
callback(param1);
}
setTimeout(function() { CallbackA() }, 1000);
}</script></head></html>";
public Form1()
{
InitializeComponent();
webBrowser1.DocumentText = javascript;
}
private void button1_Click(object sender, EventArgs e)
{
Callback cb = new Callback();
webBrowser1.Document.InvokeScript("gpsToAddress", new object[] { 123, cb });
}
}
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class Callback
{
// allows an instance of Callback to look like a function to the script
// (allows callback() rather than forcing the script to do callback.callMe)
[System.Runtime.InteropServices.DispId(0)]
public void callMe(string v_address)
{
MessageBox.Show(v_address);
}
}
}