【问题标题】:wpf webbrowser control and google maps, overlays gray imagewpf webbrowser 控件和谷歌地图,覆盖灰色图像
【发布时间】:2013-12-10 22:44:08
【问题描述】:

长期以来,我一直在使用 c# 中的 webbrowser 控件来呈现谷歌地图。最近,谷歌地图现在加载,然后覆盖了一个灰色框。看图

http://www.condoresorts.com/Debug/webdebug.png

有人知道这是什么原因造成的吗?

this.webBrowser1.DocumentText = @"<iframe width=""425"" height=""350"" frameborder=""0"" scrolling=""no"" marginheight=""0"" marginwidth=""0"" src=""https://maps.google.com/maps?f=d&amp;source=s_d&amp;saddr=Oklahoma+City,+OK&amp;daddr=texas&amp;hl=en&amp;geocode=FSgxHQIddAQw-imB0vh-VIqthzGdOk_RdBKiMw%3BFVfN5wEdi54L-ilJMoILNnBAhjE83ggYjxzrFg&amp;aq=t&amp;sll=39.632471,-56.554076&amp;sspn=61.466508,135.263672&amp;mra=ls&amp;ie=UTF8&amp;t=m&amp;z=7&amp;output=embed""></iframe><br /><small><a href=""https://maps.google.com/maps?f=d&amp;source=embed&amp;saddr=Oklahoma+City,+OK&amp;daddr=texas&amp;hl=en&amp;geocode=FSgxHQIddAQw-imB0vh-VIqthzGdOk_RdBKiMw%3BFVfN5wEdi54L-ilJMoILNnBAhjE83ggYjxzrFg&amp;aq=t&amp;sll=39.632471,-56.554076&amp;sspn=61.466508,135.263672&amp;mra=ls&amp;ie=UTF8&amp;t=m&amp;z=7"" style=""color:#0000FF;text-align:left"">View Larger Map</a></small>";

Noseratio 是正确的。我按照他的链接,实现了项目的代码,它不再有这种行为。

amespace WebBroweerControls { 公共课Form1 { System.Windows.Forms.WebBrowser webBrowser1 = null;

    public Form1(System.Windows.Forms.WebBrowser br)
    {

        webBrowser1 = br;

        SetBrowserFeatureControl();



    }

    private void Form1_Load(object sender, EventArgs e)
    {
        //DoNavigationAsync().ContinueWith(_ =>
        //{
        //    MessageBox.Show("Navigation complete!");
        //}, TaskScheduler.FromCurrentSynchronizationContext());


        //var html = "<iframe width=\"425\" height=\"350\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" " +
        //       "src=\"https://maps.google.com/maps?f=d&amp;source=s_d&amp;saddr=Oklahoma+City,+OK&amp;daddr=texas&amp;hl=en&amp;" +
        //       "geocode=FSgxHQIddAQw-imB0vh-VIqthzGdOk_RdBKiMw%3BFVfN5wEdi54L-ilJMoILNnBAhjE83ggYjxzrFg&amp;aq=t&amp;sll=39.632471," +
        //       "-56.554076&amp;sspn=61.466508,135.263672&amp;mra=ls&amp;ie=UTF8&amp;t=m&amp;z=7&amp;output=embed\">" +
        //       "</iframe>" +
        //       "<br /><small><a href=\"https://maps.google.com/maps?f=d&amp;source=embed&amp;saddr=Oklahoma+City,+OK&amp;daddr=texas" +
        //       "&amp;hl=en&amp;geocode=FSgxHQIddAQw-imB0vh-VIqthzGdOk_RdBKiMw%3BFVfN5wEdi54L-ilJMoILNnBAhjE83ggYjxzrFg&amp;aq=t&amp;" +
        //       "sll=39.632471,-56.554076&amp;sspn=61.466508,135.263672&amp;mra=ls&amp;ie=UTF8&amp;t=m&amp;z=7\" style=\"color:#0000FF;" +
        //       "text-align:left\">View Larger Map</a></small>";

        webBrowser1.Navigate("http://www.condoresorts.com/Directions/PrintShares/Coyote%20Lakes/Pillars%20to%20Coyote%20Lakes.htm");
    }

    private async Task DoNavigationAsync()
    {
        TaskCompletionSource<bool> documentCompleteTcs = null;

        WebBrowserDocumentCompletedEventHandler handler = delegate
        {
            if (documentCompleteTcs.Task.IsCompleted)
                return;
            documentCompleteTcs.SetResult(true);
        };

        documentCompleteTcs = new TaskCompletionSource<bool>();
        this.webBrowser1.DocumentCompleted += handler;

        // could do this.wb.Navigate(url) here 
        this.webBrowser1.DocumentText = "<!DOCTYPE html><head><meta http-equiv='X-UA-Compatible' content='IE=edge'/></head>" +
            "<body><input size=50 type='text' placeholder='HTML5 if this placeholder is visible'/></body>";

        await documentCompleteTcs.Task;
        this.webBrowser1.DocumentCompleted -= handler;

        dynamic document = this.webBrowser1.Document.DomDocument;
        dynamic navigator = document.parentWindow.navigator;
        var info =
            "\n navigator.userAgent: " + navigator.userAgent +
            "\n navigator.appName: " + navigator.appName +
            "\n document.documentMode: " + document.documentMode +
            "\n document.compatMode: " + document.compatMode;

        MessageBox.Show(info);
    }

    private static void SetBrowserFeatureControl()
    {
        // http://msdn.microsoft.com/en-us/library/ee330720(v=vs.85).aspx

        // WebBrowser Feature Control settings are per-process
        var fileName = System.IO.Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);

        // make the control is not running inside Visual Studio Designer
        if (String.Compare(fileName, "devenv.exe", true) == 0 || String.Compare(fileName, "XDesProc.exe", true) == 0)
            return;

        SetBrowserFeatureControlKey("FEATURE_BROWSER_EMULATION", fileName, GetBrowserEmulationMode());
    }

    private static void SetBrowserFeatureControlKey(string feature, string appName, uint value)
    {
        using (var key = Registry.CurrentUser.CreateSubKey(
            String.Concat(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\", feature),
            RegistryKeyPermissionCheck.ReadWriteSubTree))
        {
            key.SetValue(appName, (UInt32)value, RegistryValueKind.DWord);
        }
    }

    private static UInt32 GetBrowserEmulationMode()
    {
        int browserVersion = 7;
        using (var ieKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer",
            RegistryKeyPermissionCheck.ReadSubTree,
            System.Security.AccessControl.RegistryRights.QueryValues))
        {
            var version = ieKey.GetValue("svcVersion");
            if (null == version)
            {
                version = ieKey.GetValue("Version");
                if (null == version)
                    throw new ApplicationException("Microsoft Internet Explorer is required!");
            }
            int.TryParse(version.ToString().Split('.')[0], out browserVersion);
        }

        // Internet Explorer 10. Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode. Default value for Internet Explorer 10.
        UInt32 mode = 10000;

        switch (browserVersion)
        {
            case 7:
                // Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode. Default value for applications hosting the WebBrowser Control.
                mode = 7000;
                break;
            case 8:
                // Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode. Default value for Internet Explorer 8
                mode = 8000;
                break;
            case 9:
                // Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode. Default value for Internet Explorer 9.
                mode = 9000;
                break;
            default:
                // use IE10 mode by default
                break;
        }

        return mode;
    }
}

}

刚刚实例化了这个类并将它提供给我的网络浏览器控件:

WebBroweerControls.Form1 f1 = new WebBroweerControls.Form1(this.webBrowser1);

【问题讨论】:

  • 这似乎是 WinForms WebBrowser 控件,而不是 WPF 控件。请更改您的标签和标题。
  • 使用其中之一。控件大致相同,并且都产生相同的结果。
  • condoresorts.com/Debug/Untitled-3.gif 这正是浏览器所做的。我可以在 .html 文档中打开 html 并正确显示
  • @user190084,这个 HTML 在完整的 IE 浏览器下可以工作吗?如果是这样,请查看this
  • 是的,在普通浏览器下可以正常显示。我明天一进办公室就去看看这个帖子。谢谢!

标签: c# wpf webbrowser-control


【解决方案1】:

试试下面的...

 var html = "<iframe width=\"425\" height=\"350\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" " +
                   "src=\"https://maps.google.com/maps?f=d&amp;source=s_d&amp;saddr=Oklahoma+City,+OK&amp;daddr=texas&amp;hl=en&amp;" +
                   "geocode=FSgxHQIddAQw-imB0vh-VIqthzGdOk_RdBKiMw%3BFVfN5wEdi54L-ilJMoILNnBAhjE83ggYjxzrFg&amp;aq=t&amp;sll=39.632471," +
                   "-56.554076&amp;sspn=61.466508,135.263672&amp;mra=ls&amp;ie=UTF8&amp;t=m&amp;z=7&amp;output=embed\">" +
                   "</iframe>" +
                   "<br /><small><a href=\"https://maps.google.com/maps?f=d&amp;source=embed&amp;saddr=Oklahoma+City,+OK&amp;daddr=texas" +
                   "&amp;hl=en&amp;geocode=FSgxHQIddAQw-imB0vh-VIqthzGdOk_RdBKiMw%3BFVfN5wEdi54L-ilJMoILNnBAhjE83ggYjxzrFg&amp;aq=t&amp;" +
                   "sll=39.632471,-56.554076&amp;sspn=61.466508,135.263672&amp;mra=ls&amp;ie=UTF8&amp;t=m&amp;z=7\" style=\"color:#0000FF;" +
                   "text-align:left\">View Larger Map</a></small>";

 webBrowser1.NavigateToString(html);

基本相同的代码...只是使用 NavigateToString() 将 html 参数加载到 WebBrowser。

【讨论】:

  • 同样的问题。地图确实加载了,然后在顶部覆盖了一张灰色地图。请查看我的链接了解它的外观。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-03
  • 2012-12-25
  • 2012-11-11
  • 2016-04-06
  • 1970-01-01
  • 2015-09-03
相关资源
最近更新 更多