简体   繁体   English

C#WPF WebBrowser-如何将数组传递给javascript函数

[英]C# WPF WebBrowser - how to pass an array to a javascript function

I am trying to add multiple markers to a google map. 我正在尝试向Google地图添加多个标记。 The data comes from C# WPF. 数据来自C#WPF。

Here is the C# code: 这是C#代码:

private void Button_click(object sender, RoutedEventArgs e)
{
    int[] lat = { 10, 30, 50, 70 };
    int[] lon = { 10, 30, 50, 70 };

    webBrowser1.InvokeScript("addMarker", new object[] { lat, lon });
}

Here is the javascript function embedded in a local html file: 这是嵌入本地html文件中的javascript函数:

function addMarker(Lat,Long) {
    for (var i=0;i<Lat.length; i++){
        var latLng = new google.maps.LatLng(Lat[i],Long[i]);
        var marker = new google.maps.Marker({
            position: latLng,
            title: 'Hello World!',
            map: map
        });
    }
    } // end of addMarker

When I compiled this program with VS 2015 and clicked the button to invoke this js script, I always got an error message telling me that a function is expected in this line right after 'var' 当我使用VS 2015编译该程序并单击按钮以调用此js脚本时,总是收到一条错误消息,告诉我在'var'之后该行中应有一个函数

for (var i=0;i<Lat.length; i++){

The exception report by VS is like this: System.Runtime.InteropServices.COMException Exception from HRESULT: 0x80020101 and the location of the exception is right here: VS的异常报告是这样的:System.Runtime.InteropServices.COMException来自HRESULT的异常:0x80020101,异常的位置就在这里:

webBrowser1.InvokeScript("addMarker", new object[] { lat, lon });

I am very new to Javascript programming. 我对Java编程非常陌生。 Please help. 请帮忙。

As far as I know its not possible to pass an array directly. 据我所知,不可能直接传递数组。

You should pass it as a JSON-string. 您应该将其作为JSON字符串传递。 (I am using NewtonSoft via NuGet!) (我正在通过NuGet使用NewtonSoft !)

guiWebbrowser.InvokeScript("addMarker", JsonConvert.SerializeObject(new
         {
            Lat = new int[] { 10, 20, 30 },
            Long = new int[] { 10, 20, 30 }
         }));

Your JS-function should look like this: 您的JS函数应如下所示:

function addMarker(jsonArg) {
   var args = JSON.parse(jsonArg);
   for (var i = 0; i < args.Lat.length; i++) {
      var latLng = new google.maps.LatLng(args.Lat[i], args.Long[i]);
      var marker = new google.maps.Marker({
         position: latLng,
         title: 'Hello World!',
         map: map
      });
   }
} // end of addMarker

You may get this error message: JSON is undefined . 您可能会收到此错误消息: JSON is undefined

See this SO answer: https://stackoverflow.com/a/22287896/3631348 看到这样的答案: https : //stackoverflow.com/a/22287896/3631348

You should call this function anywhere in your app: 您应该在应用程序中的任何位置调用此函数:

public void ModifyBrowserEmulation(int version = 11001)
{
   // see https://stackoverflow.com/a/22287896/3631348 --> edit your application name ! or get it via Reflection !

   var appExe = System.IO.Path.GetFileName(Assembly.GetExecutingAssembly().Location);
   Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", appExe, version, RegistryValueKind.DWord);
   Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", appExe.Replace(".exe", ".vshost.exe"), version, RegistryValueKind.DWord);

   Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", appExe, version, RegistryValueKind.DWord);
   Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", appExe.Replace(".exe", ".vshost.exe"), version, RegistryValueKind.DWord);
}

Another useful post: https://stackoverflow.com/a/31728506/3631348 how to write to the console from your JS code. 另一个有用的文章: https : //stackoverflow.com/a/31728506/3631348如何从您的JS代码写入控制台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM