wpf 网页浏览器控件 - 未应用 CSS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18576963/
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 09:33:47  来源:igfitidea点击:

WebBrowser Control - No CSS applied

c#csswpfwinformswebbrowser-control

提问by bhs

I need to host an online payment gateway in a Browsercontrol in Framework 4.5and have come across the problem where the CSSis not applied correctly or indeed at all.

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

I have been through all of the options herewith no luck and have tried to use the Navigateoverride detailed hereand shown below where the page renders properly but is popped in a new browser window.

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

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

What I'm looking to do is make some webservicecalls dependent on what control the user clicks in so I have tapped into the MouseDownevent.

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

I've also tried a WPFapp with no luck to see if the Browsercontrol is different.

我也试过一个WPF没有运气的应用程序来查看Browser控件是否不同。

I'm waiting to see if the payment gateway guys can supply me with the CSS so I can apply it manually but in the meantime does anyone have any other suggestions ?

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

**** UPDATE ****

**** 更新 ****

Have tried the suggestions below with no luck.

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

I have also tried this Internet Explorer Local Machine Zone Lockdownto see if it made any differences and it didn't.

我也试过这个Internet Explorer 本地机器区域锁定,看看它是否有任何区别,但没有。

***** Further Update ***** I'm getting the following error about the certificate at this site :

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

Certificate Error

证书错误

And also a JavaScript errors advising me that AddEventis not supported. I'm wondering if this is the failed browser emulation ?

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

Another update

另一个更新

In realtion to the above I followed Noseratio's excellent advice and added the following:

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

SetBrowserFeatureControlKey("FEATURE_WARN_ON_SEC_CERT_REV_FAILED", fileName, 0); 

This feature is not supported for applications hosting the WebBrowser Control.

承载 WebBrowser 控件的应用程序不支持此功能。

回答by noseratio

Usually, implementing FEATURE_BROWSER_EMULATIONresolves issues like this, but you mentioned you already did that. I could share a test app if you like to try it with your own HTML+CSS.

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

First, try it as is, you should see something like this:

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

enter image description here

在此处输入图片说明

Note documentModeand compatModevalues, these correspond to HTML5 standard mode. Then try it with your HTML, see if they stay the same.

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