admin管理员组文章数量:1332389
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=""
xmlns:x=""
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=""></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>
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./winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft./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./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>
Share
Improve this question
edited Oct 10, 2017 at 17:01
Cœur
38.8k26 gold badges205 silver badges277 bronze badges
asked Jan 24, 2013 at 13:48
MaheshMahesh
1,4854 gold badges20 silver badges23 bronze badges
4
-
You may be calling the
Pan
function before it is loaded. Take a look at this Q: stackoverflow./questions/12641771/webbrowser-invokescript – Sam Commented Jan 24, 2013 at 13:52 - I have read the post, they said to implement the LoadCompleted event handler and then invoke your script. Can you give me some idea, how to implement LoadCompleted event handler in wpf. – Mahesh Commented Jan 24, 2013 at 13:55
- I think the easiest way is to look for the events tab when you click on your WebBrowser on the design tab in VS. – Sam Commented Jan 24, 2013 at 14:39
- You are right. I have solved the problem. Thanks you for you replay. – Mahesh Commented Jan 24, 2013 at 17:22
2 Answers
Reset to default 2Yes, 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
{
}
}
}
}
It's better to move the JavaScript invoke to Document loaded event.
本文标签:
版权声明:本文标题:WPF - Error while calling javascript function - Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME)) - Stack 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742287143a2447127.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论