WebBrowser控件 - 未应用CSS

时间:2013-09-02 15:53:41

标签: c# css wpf winforms webbrowser-control

我需要在Browser的{​​{1}}控件中托管在线支付网关,并且遇到Framework 4.5未正确应用或根本未应用的问题。

我已经浏览了所有选项here而没有运气,并尝试使用CSS覆盖详细here,并在下面显示页面正确呈现但是弹出一个新的浏览器窗口。

Navigate

我要做的是根据用户点击的控件进行一些browser.Navigate(url, "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\">"); 调用,以便我点击webservice事件。

我还尝试了一个MouseDown应用,但没有运气,看看WPF控件是否有所不同。

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

****更新****

尝试了以下建议但没有运气。

我也试过这个Internet Explorer Local Machine Zone Lockdown,看它是否有任何差异,但事实并非如此。

*****进一步更新***** 我在这个网站上收到以下关于证书的错误:

Certificate Error

还有一个JavaScript错误,告诉我Browser不受支持。我想知道这是否是失败的浏览器模拟?

另一次更新

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

AddEvent

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

1 个答案:

答案 0 :(得分:3)

通常,实施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;
        }
    }
}

首先,按原样试试,你应该看到这样的东西:

enter image description here

注意documentModecompatMode值,这些值对应于HTML5标准模式。然后尝试使用HTML,看看它们是否保持不变。