gpt4 book ai didi

javascript - InvokeScript() 方法返回 null

转载 作者:行者123 更新时间:2023-12-03 06:26:02 25 4
gpt4 key购买 nike

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。有什么问题吗??

最佳答案

您的函数 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);
}
}
}

关于javascript - InvokeScript() 方法返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38653200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com