wpf webbrowser控件和谷歌地图,叠加灰色图像

时间:2013-12-10 22:44:09

标签: c# wpf webbrowser-control

我一直在使用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;
    }
}

}

刚刚实例化了这个类并将其提供给我的webbrowser控件:

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

1 个答案:

答案 0 :(得分:0)

尝试以下方法......

 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。