【问题标题】:WebBrowser Control - No CSS appliedWebBrowser 控件 - 未应用 CSS
【发布时间】:2013-09-02 15:53:40
【问题描述】:

我需要在Framework 4.5 中的Browser 控件中托管一个在线支付网关,并且遇到了CSS 没有正确应用或根本没有应用的问题。

我已经通过所有选项 here 没有运气,并尝试使用 Navigate 覆盖详细 here 并显示如下页面正确呈现但在新浏览器窗口中弹出的位置。

browser.Navigate(url, "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\">");

我要做的是根据用户单击的控件进行一些webservice 调用,因此我已经利用了MouseDown 事件。

我还尝试了WPF 应用程序,但没有看到Browser 控件是否不同。

我正在等待支付网关人员是否可以向我提供 CSS,以便我可以手动应用它,但与此同时有人有任何其他建议吗?

**** 更新 ****

尝试了以下建议,但没有成功。

我也试过这个Internet Explorer Local Machine Zone Lockdown,看看它是否有任何不同,但没有。

***** 进一步更新 ***** 我在此站点上收到有关证书的以下错误:

还有一个 JavaScript 错误提示我不支持 AddEvent。我想知道这是否是失败的浏览器仿真?

另一个更新

针对上述内容,我遵循了 Noseratio 的出色建议并添加了以下内容:

SetBrowserFeatureControlKey("FEATURE_WARN_ON_SEC_CERT_REV_FAILED", fileName, 0); 

托管 WebBrowser 控件的应用程序不支持此功能。

【问题讨论】:

    标签: c# css wpf winforms webbrowser-control


    【解决方案1】:

    通常,实现FEATURE_BROWSER_EMULATION 可以解决此类问题,但您提到您已经这样做了。如果你想用你自己的 HTML+CSS 试用它,我可以分享一个测试应用程序。

    using Microsoft.Win32;
    using System;
    using System.Diagnostics;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WbTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                SetBrowserFeatureControl();
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                DoNavigationAsync().ContinueWith(_ =>
                {
                    MessageBox.Show("Navigation complete!");
                }, TaskScheduler.FromCurrentSynchronizationContext());
            }
    
            private async Task DoNavigationAsync()
            {
                TaskCompletionSource<bool> documentCompleteTcs = null;
    
                WebBrowserDocumentCompletedEventHandler handler = delegate 
                {
                    if (documentCompleteTcs.Task.IsCompleted)
                        return;
                    documentCompleteTcs.SetResult(true);
                };
    
                documentCompleteTcs = new TaskCompletionSource<bool>();
                this.wb.DocumentCompleted += handler;
    
                // could do this.wb.Navigate(url) here 
                this.wb.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.wb.DocumentCompleted -= handler;
    
                dynamic document = this.wb.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;
            }
        }
    }
    

    首先,按原样尝试,您应该会看到如下内容:

    注意documentMode 和compatMode 值,这些对应于HTML5 标准模式。然后用你的 HTML 试试,看看它们是否保持不变。

    【讨论】:

    • 感谢 Noseratio。我尝试了你的建议仍然没有运气。我的代码与您的代码略有不同,因为我不进行异步调用。但是,我确实得到了与您的消息框中相同的值。我需要在浏览器窗口中捕获鼠标事件,所以不希望它完全显示在新窗口中。它检查它是否在 Visual Studio 中运行的原因是什么?如果我要使用 SSL 地址,会有什么不同吗?另外 - 机器被锁定在这里,IE 也被锁定 - 这会有所不同吗?
    • 我在我的原始帖子中添加了一条评论,以确保所有尝试过的东西都被捕获在同一个地方。
    • 您的 HTML 内容是否通过 file:// 协议在本地提供?
    • 我检查 VS 进程以避免将它们的名称放在注册表中以进行 WB 功能控制(例如,VS 设计器可以是一个单独的进程,它仍然运行您的 C# 代码)。 HTTPS:// 应该没有区别(但 FILE:// 默认情况下是不行的)。试试其他fetures,也许你会找到适合你的组合。基本上,规则是,默认情况下将each feature 设置为与完整 IE 浏览器相同的值。
    • 帮了大忙
    猜你喜欢
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 2012-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多