[英]Array of JS objects to VB.Net
Bit of a doozy here. 这里有点笨拙。 I've got various COM exposed systems and I've implemented Google maps (v3) into my .net software. 我有各种COM公开系统,并且已经在我的.net软件中实现了Google地图(v3)。 What I'm trying to do now, is that when a user edits a polygon (defining an area), I send back all path points to .net for storing into our database. 我现在想做的是,当用户编辑多边形(定义区域)时,我将所有路径点发送回.net以存储到我们的数据库中。
My problem is that .Net knows that the JS array I pass back is X elements in size, but for the life of me I can't figure out how to reference the values while iterating through the array. 我的问题是.Net知道我传回的JS数组的大小是X个元素,但是对于我一生来说,我不知道如何在遍历数组时引用值。
Here's the .NET (VB) method I'm using from JS with window.external
这是我正在window.external
从JS使用的.NET(VB)方法
Public Sub AreaPointMoved(ByRef obj As Object, ByVal s As String)
MsgBox(s) ' contains a string of lat/lngs from JS
MsgBox(obj.length)
For i As Integer = 0 To obj.length
MsgBox(obj(i).lat & ", " & obj(i).lng) ' this doesn't work
'MsgBox(obj.lat & ", " & obj.lng) ' this doesn't work
Next
End Sub
And the JS that's sending stuff back upon the set_at
event being triggered: 触发set_at
事件后发送set_at
的JS:
function DrawAreaOverlay(area, col)
{
var coordsString = "";
var areaCoords = [];
overlayLayer[overlayCount] = new Array();
for(var j=0; j<area.Count; j++)
{
areaCoords.push(new google.maps.LatLng(area.Item(j).lng, area.Item(j).lat));
}
var poly = new google.maps.Polygon({
paths: areaCoords,
strokeColor: col,
strokOpacity: 0.35,
strokeWeight: 2,
fillColor: col,
fillOpacity: 0.25,
geodesic: false,
editable: canEdit,
draggable: canDrag,
map: map
});
overlayLayer[overlayCount].push(poly);
poly.getPaths().forEach(function(path, index){
google.maps.event.addListener(path, 'set_at', function(){
var arrayOfPoints = new Array();
var g = new Array();
arrayOfPoints = poly.getPath();
coordsString = "";
for(var i=0; i<arrayOfPoints.length; i++)
{
//simpleArray[i] = poly.getPath().getAt(i).lat() + ", " + poly.getPath().getAt(i).lng();
geoObj = new Object();
geoObj.lat = poly.getPath().getAt(i).lat();
geoObj.lng = poly.getPath().getAt(i).lng();
g.push(geoObj);
coordsString += poly.getPath().getAt(i).lat() + ", " + poly.getPath().getAt(i).lng() + "|";
}
window.external.AreaPointMoved(g, coordsString);
//alert(path.getLength());
});
});
}
I'm really confused. 我真的很困惑 Getting objects from .net to JS was a doddle. 将对象从.net传输到JS是一件容易的事。 But I can't for the life of me figure out what I'm doing wrong on the reverse :( 但是我无法终生弄清楚我在做错的事情:(
Cheers. 干杯。
According to answer like this , you have two options at your disposal for interacting with non-primitive arguments with calls to your ObjectForScripting
. 据回答这样的 ,你有两个选择在您的处置与非基本参数与调用您的互动ObjectForScripting
。 This Connect thread says you should use a dynamic
as the argument type. 这个Connect线程说您应该使用dynamic
变量作为参数类型。
To my surprise, I learned that VB.NET doesn't have a an equivalent to C#'s dynamic keyword . 令我惊讶的是,我了解到VB.NET没有等效于C#的dynamic关键字 。 Apparently Option Strict Off
and Option Infer On
accomplish the same ends: 显然, Option Strict Off
和Option Infer On
可以达到相同的目的:
Option Strict Off
Option Infer On
Public Sub AreaPointMoved(ByRef obj As Object, ByVal s As String)
For i As Integer = 0 To obj.length
'late binding to .lat and .lng should work now
' open Debug > Windows > Immediate
Debug.Print(obj(i).lat)
Debug.Print(obj(i).lng)
Next
End Sub
This is untested as I can't easily test any of this code. 这是未经测试的,因为我无法轻松地测试任何代码。 If this doesn't work, the second hail mary is using reflection to invoke the property: 如果这不起作用,则第二个冰雹玛丽正在使用反射调用该属性:
Public Sub AreaPointMoved(ByRef obj As Object, ByVal s As String)
Dim lat As Double
Dim lng As Double
For i As Integer = 0 To obj.length
lat = obj(i).GetType().InvokeMember("lat", BindingFlags.InvokeMethod, Nothing, obj(i), Nothing)
lng = obj(i).GetType().InvokeMember("lng", BindingFlags.InvokeMethod, Nothing, obj(i), Nothing)
' view in Debug > Windows > Immediate
Debug.Print(lat)
Debug.Print(lng)
Next
End Sub
Forgive my VB.NET for any syntax errors. 原谅我的VB.NET的任何语法错误。 My skills translating C# to VB.NET are rusty at best. 我将C#转换为VB.NET的技能充其量是生锈的。
Have you thought about using an array of arrays of doubles? 您是否考虑过使用双精度数组的数组? You should not need an object property just to pass coordinates: 您只需要传递坐标就不需要object属性:
Public Sub AreaPointMoved(ByVal coordinates()() As Double)
End Sub
for(var i=0; i<arrayOfPoints.length; i++)
{
g.push([poly.getPath().getAt(i).lat(), poly.getPath().getAt(i).lng()]);
}
window.external.AreaPointMoved(g);
Hopefully one of these will help you sort this out. 希望其中之一可以帮助您解决此问题。 Good luck! 祝好运!
Sidebar: I highly recommend using Chromium Embedded Framework (CEF) or possibly Awesomium over the Windows WebBrowser control (which is a COM interop wrapper around Internet Explorer). 补充工具栏:我强烈建议在Windows WebBrowser控件(Internet Explorer周围的COM互操作包装器)上使用Chromium嵌入式框架(CEF)或可能的Awesomium 。 Embedding Google Maps into a desktop app is going to be way smoother with a browser based on Chromium over IE. 使用基于Chromium over IE的浏览器,将Google Maps嵌入桌面应用程序将变得更加流畅。 Both CEF and Awesomium have much richer ways to accomplish bi-directional calls between .Net IL and Javascript. CEF和Awesomium都有更丰富的方法来完成.Net IL和Javascript之间的双向调用。 You are having a hard time with the window.external
and WebBrowser.ObjectForScripting
simply because it is a crappy API for anything serious. 您在使用window.external
和WebBrowser.ObjectForScripting
时遇到了window.external
,这仅仅是因为它对于任何严重的事情都是a脚的API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.