WPF - Error while calling javascript function - Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

I am using "WebBroswer" in my wpf application to render Google map. So I am calling Pan(x, y) JavaScript method with some parameters form my c# code.

But I am getting this below error.

Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))

My Window2.xaml File:

<Window x:Class="Test.Window2"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     Title="Window2" Height="300" Width="300">     <Grid>         <WebBrowser Name="mapBrowser" Margin="50" />         <Button Name="button1" VerticalAlignment="Bottom" Click="button1_Click">Button</Button>     </Grid> </Window> 

My Window2.xaml.cs File:

namespace Test {     /// <summary>     /// Interaction logic for Window2.xaml     /// </summary>     public partial class Window2 : Window     {         public Window2()         {             InitializeComponent();         }          private void button1_Click(object sender, RoutedEventArgs e)         {             Uri uri = new Uri(@"pack://application:,,,/HTMLPage1.htm");             Stream source = Application.GetContentStream(uri).Stream;             mapBrowser.NavigateToStream(source);             this.mapBrowser.InvokeScript("Pan", x, y);         }     } } 

My HTML Page:

<html> <head>     <title></title>     <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />      <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>      <script type="text/javascript">         var map;         var latlng;         var lat;         var lng;         var zoom;          function Init() {             lat = 40.632915;             lng = -8.68929;             zoom = 18             latlng = new google.maps.LatLng(lat, lng);             var options = {                 zoom: 18,                 center: latlng,                 mapTypeId: google.maps.MapTypeId.SATELLITE             };              map = new google.maps.Map(document.getElementById("map_canvas"), options);         }          function Pan(x, y) {             try {                 lat = x;                 lng = y;                  map.panTo(new google.maps.LatLng(lat, lng));             }             catch (ex) {                 window.alert("Pan:Error");             }         }     </script>  </head> <body onload="Init()">     <div id="map_canvas" style="width: 100%; height: 100%">     </div> </body> </html> 

回答1:

Yes, I got the problem. I try to call the JavaScript function without loading the HTML page. So I have first loaded the html page and then called the JavaScript function and its working for me.

So I have changed my code as below and it is working fine :-

namespace WpfApplication1 {     /// <summary>     /// Interaction logic for Window1.xaml     /// </summary>     public partial class Window1 : Window     {         public Window1()         {             InitializeComponent();             Uri uri = new Uri(@"pack://application:,,,/HTMLPage1.htm");             Stream source = Application.GetContentStream(uri).Stream;             mapBrowser.NavigateToStream(source);         }          private void button1_Click(object sender, RoutedEventArgs e)         {             try             {                    this.mapBrowser.InvokeScript("Pan", 19.006145, 72.818148);             }             catch             {              }         }     } } 


回答2:

It's better to move the JavaScript invoke to Document loaded event.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!