2016-07-29 5 views
0

Die InvokeScript() - Methode gibt null zurück.Die InvokeScript() - Methode gibt null zurück

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

und 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); 
} 

die 'args' und 'v_address' null zurück und die messageBox null zurück, auch. Ich möchte einige Werte zurückgeben. Bitte, hilf mir!

EDIT: OK, ich bearbeitet den C# -Code wie folgt aus:

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

Also, args ist nicht null, aber v_address ist immer noch null. Was ist das Problem??

+0

irgendwelche Lösungen ?? –

Antwort

0

Ihre Funktion gpsToAddress gibt nichts zurück. Aber es rief eine Callback-Funktion auf, wenn der Prozess beendet ist.

Ich empfehle Ihnen, die Rückruffunktion übergeben, wenn Sie die Funktion aufgerufen haben.

Bitte schauen Sie diesen Thread für weitere Informationen: Make async event synchronous in JavaScript

Edit: auf die obige Frage Basierend:

einen Rückruf erstellen Klasse

[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); 
    } 
} 

Anruf js-Funktion mit einem Rückruf:

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

Die js-Funktion nimmt einen Callbac k als Argument:

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

Edit 2: (Blindprobe)

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); 
    } 
} 
} 
+0

Danke für Ihre Antwort. Aber ich verstehe es nicht. Könntest du mehr erklären? –

+0

OK, ich habe verstanden. Aber 'WindowsFormsApplication6.Callback' wird zurückgegeben. Daher wird die Ausnahme "WindowsFormsApplication6 nicht definiert" ausgelöst. –

+0

In Ihrem Code ist ein Fehler aufgetreten. Ich habe eine Dummy-Probe in neue Bearbeitung – Silvinus