C#WebBrowser控件不应用css

Jam*_*esL 8 css c# webbrowser-control

我有一个我正在VS2005中工作的项目.我添加了一个WebBrowser控件.我向控件添加了一个基本的空页面

private const string _basicHtmlForm = "<html> "
                                      + "<head> "
                                      + "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/> "
                                      + "<title>Test document</title> "
                                      + "<script type='text/javascript'> "
                                      + "function ShowAlert(message) { "
                                      + "   alert(message); "
                                      + "} "
                                      + "</script> "
                                      + "</head> "
                                      + "<body><div id='mainDiv'> "
                                      + "</div></body> "
                                      + "</html> ";

private string _defaultFont = "font-family: Arial; font-size:10pt;";

private void LoadWebForm()
{
    try 
    {
        _webBrowser.DocumentText = _basicHtmlForm;
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}  
Run Code Online (Sandbox Code Playgroud)

然后通过dom添加各种元素(使用_webBrowser.Document.CreateElement).我也在加载一个css文件:

private void AddStyles()
{
    try
    {
        mshtml.HTMLDocument currentDocument = (mshtml.HTMLDocument) _webBrowser.Document.DomDocument;
        mshtml.IHTMLStyleSheet styleSheet = currentDocument.createStyleSheet("", 0);

        TextReader reader = new StreamReader(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath),"basic.css"));
        string style = reader.ReadToEnd();
        styleSheet.cssText = style;
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是css页面内容:

body {
    background-color: #DDDDDD;
}

.categoryDiv {
    background-color: #999999;
}

.categoryTable {
    width:599px; background-color:#BBBBBB;
}

#mainDiv {
    overflow:auto; width:600px;
}
Run Code Online (Sandbox Code Playgroud)

样式页面已成功加载,但页面上受影响的唯一元素是最初位于页面(body和mainDiv)中的元素.我也尝试将css包含在标题部分的元素中,但它仍然只影响创建页面时的元素.

所以我的问题是,有没有人知道为什么css没有应用于页面加载后创建的元素?在添加完所有元素之后,我也尝试过不应用css,但结果不会改变.

And*_*tus 6

我对你的AddStyles()方法进行了一些修改,它对我有用.你在哪里叫它?我从"_webBrowser_DocumentCompleted"调用它.

我必须指出,在修改DOM之后,我正在调用AddStyles.

private void AddStyles()
{
    try
    {
        if (_webBrowser.Document != null)
        {
            IHTMLDocument2 currentDocument = (IHTMLDocument2)_webBrowser.Document.DomDocument;

            int length = currentDocument.styleSheets.length;
            IHTMLStyleSheet styleSheet = currentDocument.createStyleSheet(@"", length + 1);
            //length = currentDocument.styleSheets.length;
            //styleSheet.addRule("body", "background-color:blue");
            TextReader reader = new StreamReader(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "basic.css"));
            string style = reader.ReadToEnd();
            styleSheet.cssText = style;

        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的DocumentCompleted处理程序(我在basic.css中添加了一些样式用于测试):

private void _webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
  HtmlElement element = _webBrowser.Document.CreateElement("p");
  element.InnerText = "Hello World1";
  _webBrowser.Document.Body.AppendChild(element);

  HtmlElement divTag = _webBrowser.Document.CreateElement("div");
  divTag.SetAttribute("class", "categoryDiv");
  divTag.InnerHtml = "<p>Hello World2</p>";
  _webBrowser.Document.Body.AppendChild(divTag);


  HtmlElement divTag2 = _webBrowser.Document.CreateElement("div");
  divTag2.SetAttribute("id", "mainDiv2");
  divTag2.InnerHtml = "<p>Hello World3</p>";
  _webBrowser.Document.Body.AppendChild(divTag2);

  AddStyles();
}
Run Code Online (Sandbox Code Playgroud)

这就是我所得到的(修改了风格,使其像单个人一样难以实现:D):

alt text http://www.freeimagehosting.net/uploads/2e372f7276.png


Ali*_*deh 0

除非您发送此链接,否则很难说。

但通常执行与样式相关的内容的最佳方法是页面中已有 css,并且在 C# 代码中只需向元素添加 id 或类即可查看样式效果。