Html .Net WebBrowser.DocumentText 没有改变!

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

.Net WebBrowser.DocumentText Isn't Changing!

.nethtmlbrowser

提问by Joe Morgan

In my vb.net program, I am using a webbrowser to show the user an HTML preview. I was previously hitting a server to grab the HTML, then returning on an asynchronous thread and raising an event to populate the WebBrowser.DocumentText with the HTML string I was returning.

在我的 vb.net 程序中,我使用网络浏览器向用户显示 HTML 预览。我以前访问服务器以获取 HTML,然后在异步线程上返回并引发事件以使用我返回的 HTML 字符串填充 WebBrowser.DocumentText。

Now I set it up to grab all of the information on the client, without ever having to hit the server, and I'm trying to raise the same event. I watch the code go through, and it has the HTML string correct and everything, but when I try to do

现在我将它设置为获取客户端上的所有信息,而不必访问服务器,并且我正在尝试引发相同的事件。我看着代码通过,它有正确的 HTML 字符串和一切,但是当我尝试做

browser.DocumentText = _emailHTML

the contents of DocumentText remain as "<HTML></HTML>"

DocumentText 的内容保持为“ <HTML></HTML>

I was just wondering why the DocumentText was not being set. Anyone have any suggestions?

我只是想知道为什么没有设置 DocumentText。有人有什么建议吗?

回答by David Mohundro

Try the following:

请尝试以下操作:

browser.Navigate("about:blank");
HtmlDocument doc = browser.Document;
doc.Write(String.Empty);
browser.DocumentText = _emailHTML;

I've found that the WebBrowsercontrol usually needs to be initialized to about:blankanyway. The same needs to be done between navigates to different types of content (like text/xml to text/html) because the renderer is different (mshtml for text/html, something else for text/xml).

我发现WebBrowser控件通常需要初始化为about:blank无论如何。在导航到不同类型的内容(例如 text/xml 到 text/html)之间需要做同样的事情,因为渲染器是不同的(对于 text/html 是 mshtml,对于 text/xml 是别的)。

(via http://geekswithblogs.net/paulwhitblog/archive/2005/12/12/62961.aspx)

(通过http://geekswithblogs.net/paulwhitblog/archive/2005/12/12/62961.aspx

回答by Matthias

I found the following and it worked!

我发现了以下内容并且它起作用了!

    webBrowser.Navigate("about:blank");
    webBrowser.Document.OpenNew(false);
    webBrowser.Document.Write(html);
    webBrowser.Refresh();

回答by FreddieH

I found the best way to handle this, is as follows:

我发现处理这个问题的最佳方法如下:

if (this.webBrowser1.Document == null)
{
    this.webBrowser1.DocumentText = htmlSource;
}
else
{
    this.webBrowser1.Document.OpenNew(true);
    this.webBrowser1.Document.Write(htmlSource);
}

回答by Interferank

That worked for me:

这对我有用:

webBrowser.Navigate("about:blank");
webBrowser.Document?.Write(htmlString);

回答by johnc

Just spotted this in some of our old code.

刚刚在我们的一些旧代码中发现了这一点。

_webBrowser.DocumentText = builder.WriteToString( ... );

Application.DoEvents();

Apparently a DoEvents also kicks the browser into rendering

显然,DoEvents 也会让浏览器进入渲染状态

回答by JOE SKEET

回答by AVIDeveloper

While Application.DoEvents()fix it in a WinForms project, it was irrelevant in a WPF project.

虽然Application.DoEvents()在 WinForms 项目中修复它,但它在 WPF 项目中无关紧要。

I finally got it to work by using webBrowser.Write( htmlContent )(instead of webBrowser.DocumentText = htmlContent).

我终于通过使用webBrowser.Write( htmlContent )(而不是webBrowser.DocumentText = htmlContent)让它工作了。

回答by Prads

This always works

这总是有效

using mshtml;


private IHTMLDocument2 Document
{
    get
    {
        if (Browser.Document != null)
        {
            return Browser.Document.DomDocument as IHTMLDocument2;
        }

        return null;
    }
}


if (Document == null)
{
    Browser.DocumentText = Contents;
}
else
{
    Document.body.innerHTML = Contents;
}

回答by antgraf

Make sure that you do not cancel Navigatingevent of WebBrowserfor about:blankpage. WebBrowsernavigates to about:blankbefore setting DocumentText. So if you want to handle links by yourself you need to create following handler of Navigatingevent:

请确保您没有取消WebBrowserabout:blank页面导航事件。WebBrowser在设置DocumentText之前导航到about:blank。因此,如果您想自己处理链接,则需要创建以下Navigating事件处理程序:

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    if(e.Url.OriginalString.StartsWith("about:"))
    {
        return;
    }
    e.Cancel = true;
    // ...
}