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

I am using "WebBroswer" in my wpf application to render Google map. So I am calling Pan(x, y)

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
Add a ment  | 

2 Answers 2

Reset to default 2

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742287144a4415520.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信