在保留自定义类型结构的情况下,从InvokeScript获取返回到C#的JavaScript自定义类型 [英] Getting JavaScript custom types returned to C# from InvokeScript with the custom type structure preserved

查看:128
本文介绍了在保留自定义类型结构的情况下,从InvokeScript获取返回到C#的JavaScript自定义类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在WebBrowser控件上使用InvokeScript,尝试使用适用于HTML的Google Maps JavaScript API制作简单的地图.我在JavaScript的HTML页面中编写了一些函数来处理地图,并且已经在C#的WPF应用程序中复制了这些方法并对其进行了编辑,以便它们调用方法并执行与JavaScript接口所需的操作.

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 JavaScript to do things with the map and I have duplicated those methods in my WPF application in C# and edited them so they call methods and do what they need to do to interface with the JavaScript.

我遇到了问题,因为并非JavaScript所返回的地图的所有值都是简单的单值类型(例如字符串,整数或布尔值),并且返回了自定义类型(例如LatLng),而且我不知道如何保留"返回值作为自定义类型的单独部分".为了帮助解释更多:

I am having a problem because not all of the values returned by the javascript for the map are simple, single value type like strings, integers or booleans and return custom types like LatLng and I don't know how to 'preserve' the returned value as separate 'parts' of the custom type. To help explain more:

通常我可以使用以下代码:

Normally I can use the following code:

return Convert.ToInt16(browser.InvokeScript("getHeading"));

在JavaScript中调用以下函数:

Which calls the following function in JavaScript:

function GetHeading() {
    return map.getHeading();
}

,并为地图的当前标题属性返回一个简单的整数值.这很容易,并且没有问题.但是某些函数具有自定义的返回类型.

And returns a simple integer value for the current heading property of the map. This is easy and there is no problem with this. But some functions have a return type that is custom.

LatLng类型由纬度值和经度值组成,因此它不能返回简单的值,如果我在本文的第一个代码段中尝试使用它,则会引发错误.对于以下功能:

The LatLng type is made up of a latitude value and a longitude value so it can't return a simple value and if I try it with the first code snippet of this post then it throws an error. For some functions like the following one:

function GetCentre() {
    return map.getCenter();
}

返回一个LatLng,其中包含地图中心的纬度和经度.我如何保存返回的值并将其保留为纬度和经度,然后将其放入C#中的自定义类型中,如下所示:

Returns a LatLng containing the latitude and longitude of the centre of the map. How can I preserve the returned value and keep it as the latitude and longitude WHICH WILL THEN be put into a custom type in C# like this:

public class LatLng
{
    public string latitude { get; set }
    public string longitude { get; set; }
}

我是否需要在javascript端编写两个单独的函数?:一个返回中心的纬度,另一个返回中心的经度.基本上,我想做的是在WebBrowser控件上使用InvokeScript,调用返回自定义类型的javascript函数,并将值保留在该自定义类型中,然后可以在C#中使用该值,例如returnValue.latitude和returnValue.longitude.谢谢,我该怎么办?

Do I have to write two separate functions on the javascript side?: One to return the latitude of the centre and another to return the longitude of it. Basically what I want to do is using InvokeScript on the WebBrowser control, call the javascript function that returns a custom type and keep the value in the custom type which I can then use in C# like returnedValue.latitude and returnedValue.longitude. How can I do this, thanks?

推荐答案

如提名所述:

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和res.lng已经加倍了.

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

这篇关于在保留自定义类型结构的情况下,从InvokeScript获取返回到C#的JavaScript自定义类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆