WebBrowser控件 - 未应用CSS [英] WebBrowser Control - No CSS applied

查看:258
本文介绍了WebBrowser控件 - 未应用CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Framework 4.5 中的浏览器控制中托管网上支付网关,并遇到了问题

我已经通过所有的选项这里没有运气,并试图使用 Navigate 重写详细这里,如下所示页面正常显示,但在新的浏览器窗口中弹出。

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

我要做的是让一些 webservice 调用依赖于用户点击什么控件,所以我已经进入了 MouseDown 事件。



我也试过了一个没有运气的 WPF 应用程序,看看 控制是不同的。



我在等待看看支付网关人可以提供我的CSS,所以我可以手动应用,


$ b

**** UPDATE ****



有任何其他建议吗?尝试下面的建议没有运气。



我也试过这个 Internet Explorer本地计算机区域锁定,看看是否有任何差异,但没有。



*****更新*****
我在此网站获得以下错误:





还有一个JavaScript错误我不支持 AddEvent



>实际上,我遵循了Noseratio的优秀建议并添加了以下内容:

  SetBrowserFeatureControlKey(FEATURE_WARN_ON_SEC_CERT_REV_FAILED,fileName,0) ; 

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


<通常,实现 FEATURE_BROWSER_EMULATION 可以解决这样的问题,但是你提到你已经这样做了。我可以共享一个测试应用程序,如果你喜欢尝试它与自己的HTML + CSS。

 使用Microsoft.Win32; 
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Forms;

命名空间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任务DoNavigationAsync()
{
TaskCompletionSource< bool> documentCompleteTcs = null;

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

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

//可以做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 =是可见的'/>< / body>;

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

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

MessageBox.Show(info);
}

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

// WebBrowser功能控制设置是per-process
var fileName = System.IO.Path.GetFileName(Process.GetCurrentProcess()。MainModule.FileName);

//使控件在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);
}
}

私人静态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.包含基于标准的网页!DOCTYPE伪指令以IE10标准模式显示。 Internet Explorer 10的默认值。
UInt32 mode = 10000;

switch(browserVersion)
{
case 7:
//包含基于标准的网页!DOCTYPE伪指令以IE7标准模式显示。托管WebBrowser控件的应用程序的默认值。
mode = 7000;
break;
case 8:
//包含基于标准的网页!DOCTYPE伪指令以IE8模式显示。 Internet Explorer 8的默认值
mode = 8000;
break;
case 9:
// Internet Explorer 9.包含基于标准的网页!DOCTYPE指令以IE9模式显示。 Internet Explorer 9的默认值。
mode = 9000;
break;
默认值:
//默认使用IE10模式
break;
}

返回模式;
}
}
}

,您应该会看到这样的内容:





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


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

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

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

What I'm looking to do is make some webservice calls dependent on what control the user clicks in so I have tapped into the MouseDown event.

I've also tried a WPF app with no luck to see if the Browser control is different.

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 ?

**** UPDATE ****

Have tried the suggestions below with no luck.

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

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

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

Another update

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

SetBrowserFeatureControlKey("FEATURE_WARN_ON_SEC_CERT_REV_FAILED", fileName, 0); 

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

解决方案

Usually, implementing FEATURE_BROWSER_EMULATION resolves 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.

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:

Note documentMode and compatMode values, these correspond to HTML5 standard mode. Then try it with your HTML, see if they stay the same.

这篇关于WebBrowser控件 - 未应用CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆