WPF - 在调用javascript函数错误 - 未知的名称。 (从HRESULT异常:0x80020006(DISP_E_UNKNOWNNAME)) [英] WPF - Error while calling javascript function - Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))
问题描述
我是新来WPF。我使用WebBroswer在我的WPF应用程序来渲染谷歌地图。于是我打电话潘(X,Y)JavaScript方法有一些参数形成我的C#code。
I am new to wpf. 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.
但我收到此错误以下
Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))
我Window2.xaml文件: -
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>
我Window2.xaml.cs文件: -
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 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>
请任何一个可以帮助我。它迫切。
Please can any one will help me. Its Urgent.
推荐答案
是的,我有这个问题。我尝试调用JavaScript函数,而无需加载HTML页面。所以我第一次加载HTML页面,然后调用的JavaScript函数和它的工作对我来说。
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.
所以我改变了我的code如下,这是工作的罚款: -
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
{
}
}
}
}
这篇关于WPF - 在调用javascript函数错误 - 未知的名称。 (从HRESULT异常:0x80020006(DISP_E_UNKNOWNNAME))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!