2013-01-24 32 views
5

Tôi đang sử dụng "WebBroswer" trong ứng dụng wpf của mình để hiển thị bản đồ Google. Vì vậy, tôi gọi phương thức JavaScript Pan (x, y) với một số tham số tạo thành mã C# của tôi.WPF - Lỗi khi gọi hàm javascript - Tên không xác định. (Ngoại lệ từ HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))

Nhưng tôi nhận được lỗi dưới đây.

Tên không xác định. (Ngoại lệ từ 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); 
     } 
    } 
} 

HTML My 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> 
+0

Bạn có thể đang gọi hàm 'Pan' trước khi được tải. Hãy xem Q: http://stackoverflow.com/questions/12641771/webbrowser-invokescript – Sam

+0

Tôi đã đọc bài đăng, họ đã nói để triển khai trình xử lý sự kiện LoadCompleted và sau đó gọi tập lệnh của bạn. Bạn có thể cho tôi một số ý tưởng, làm thế nào để thực hiện xử lý sự kiện LoadCompleted trong wpf. – Mahesh

+0

Tôi nghĩ cách dễ nhất là tìm tab sự kiện khi bạn nhấp vào Trình duyệt web của bạn trên tab thiết kế trong VS. – Sam

Trả lời

2

Có, tôi gặp sự cố. Tôi cố gắng gọi hàm JavaScript mà không cần tải trang HTML. Vì vậy, trước tiên tôi đã tải trang html và sau đó được gọi là hàm JavaScript và nó hoạt động cho tôi.

Vì vậy, tôi đã thay đổi mã của tôi như dưới đây và nó làm việc tốt: -

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 
      { 
      } 
     } 
    } 
} 
1

Nó tốt hơn để di chuyển gọi hoạt Javascript để Tài liệu sự kiện nạp.

Các vấn đề liên quan