Windows 窗体 C# 项目中的嵌入式 Web 浏览器

发布于 2024-11-14 20:31:16 字数 369 浏览 5 评论 0原文

我有一个带有嵌入式 Web 浏览器控件的表单。我目前正在使用 WebBrowser 并像这样使用它:

webBrowser1.Navigate("about:blank");
HtmlDocument doc = this.webBrowser1.Document;
doc.Write(string.Empty);
String htmlContent = GetHTML();
doc.Write(htmlContent);

这会将 HTML 正确写入 Web 浏览器控件,但它永远不会清除现有数据,它只是附加,所以我最终会得到 N 个网页堆叠在彼此之上。

这是最好的控件吗?如果是这样,为什么不清除现有数据?

I have a form with an embedded web browser control on it. I am currently using WebBrowser and use it like so:

webBrowser1.Navigate("about:blank");
HtmlDocument doc = this.webBrowser1.Document;
doc.Write(string.Empty);
String htmlContent = GetHTML();
doc.Write(htmlContent);

This writes the HTML correctly to the web browser control BUT it never clears the existing data and it just appends, so I end up with N web pages stacked on top of each other.

Is this the best control to use? If so why is it not clearing existing data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉梦枕江山 2024-11-21 20:31:16

你需要使用:

HtmlDocument doc = this.webBrowser1.Document.OpenNew(true);

现在在写入之前会清除文档的内容。

所有对 Write 的调用都应先于
通过调用 OpenNew,这将清除
当前文件及其所有内容
变量。您对 的调用写
在其中创建一个新的 HTML 文档
地方。仅更改特定的
文档的一部分,获取
适当的 HtmlElement 并设置其
InnerHtml 属性。

You need to use:

HtmlDocument doc = this.webBrowser1.Document.OpenNew(true);

now the contents of the document will be cleared before writing.

All calls to Write should be preceded
by a call to OpenNew, which will clear
the current document and all of its
variables. Your calls to Write will
create a new HTML document in its
place. To change only a specific
portion of the document, obtain the
appropriate HtmlElement and set its
InnerHtml property.

饮惑 2024-11-21 20:31:16

是的。

如果需要清除内容,您应该能够调用 Clear 方法。

查看本文以获取深入的详细信息和示例代码:
http://www.codeproject.com/KB/miscctrl/simplebrowserformfc.aspx

Yes, it is.

You should be able to call the Clear method if you need to clear contents.

Check this article for in-depth details and sample code:
http://www.codeproject.com/KB/miscctrl/simplebrowserformfc.aspx

染墨丶若流云 2024-11-21 20:31:16

在页面之间调用 HtmlDocument.OpenNew

OpenNew会清除之前加载的
文件,包括任何相关的
状态,例如变量。它不会
导致 WebBrowser 中的导航事件
待提高。

Call HtmlDocument.OpenNew between pages:

OpenNew will clear the previous loaded
document, including any associated
state, such as variables. It will not
cause navigation events in WebBrowser
to be raised.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文