WPF - 调用 javascript 函数时出错 - 名称未知。(来自 HRESULT 的异常:0x80020006 (DISP_E_UNKNOWNNAME))

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14502898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:12:45  来源:igfitidea点击:

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

javascriptwpfgoogle-mapswpf-controlsbrowser

提问by Mahesh

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.

我在 wpf 应用程序中使用“WebBroswer”来呈现 Google 地图。所以我正在调用 Pan(x, y) JavaScript 方法,其中包含一些来自我的 c# 代码的参数。

But I am getting this below error.

但我收到以下错误。

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

未知名称。(来自 HRESULT 的异常:0x80020006 (DISP_E_UNKNOWNNAME))

My Window2.xaml File:

我的 Window2.xaml 文件:

<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:

我的 Window2.xaml.cs 文件:

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 页面:

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

采纳答案by Mahesh

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.

是的,我遇到了问题。我尝试在不加载 HTML 页面的情况下调用 JavaScript 函数。所以我首先加载了 html 页面,然后调用了 JavaScript 函数,它对我有用。

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

回答by Joe

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

最好将 JavaScript 调用移动到文档加载事件。