Getting JavaScript custom types returned to C# from InvokeScript with the custom type structure preserved

后端 未结 2 723
粉色の甜心
粉色の甜心 2021-01-24 01:12

I am using InvokeScript on a WebBrowser control to try and make a simple map using the Google Maps JavaScript API for HTML. I have a few functions written in the HTML page in Ja

相关标签:
2条回答
  • 2021-01-24 01:38

    Simply use dynamic feature of C# and .NET DLR, and return the JavaScript structures as is:

    dynamic data = webBrowser1.Document.InvokeScript("eval", new[] { 
        "(function() { return { latitude: 1, longitude: 2 }; })()" });
    
    MessageBox.Show("Data: " + data.latitude + ", " + data.longitude);
    
    0 讨论(0)
  • 2021-01-24 01:49

    As Noseration said:

    JavaScript :
    
        function getCameraPosition() {
          return map.getCenter().toJSON();
        }
    
    C# :
        object[] o = new object[] {};
        dynamic res = googleMapWB.Document.InvokeScript("getCameraPosition", o);
        Console.WriteLine(res.lat+","+res.lng);
    

    res.lat and res.lng is already double in this case.

    0 讨论(0)
提交回复
热议问题