WebBrowser.Document.Body 始终为 null
我有一个 WebBrowser
文档设置为编辑模式。我试图使用 WebBrowser.Document.Body.InnerText
来操作 body 元素的内部文本,但是 WebBrowser.Document.Body
仍然为空。
这是我创建文档内容的代码:
private WebBrowser HtmlEditor = new WebBrowser();
public HtmlEditControl()
{
InitializeComponent();
HtmlEditor.DocumentText = "<html><body></body></html>";
myDoc = (IHTMLDocument2)HtmlEditor.Document.DomDocument;
myDoc.designMode = "On";
HtmlEditor.Refresh(WebBrowserRefreshOption.Completely);
myContentsChanged = false;
}
我可以编辑代码,一切都很好,但我不明白为什么 HtmlEditor.Document.Body
保持为空。我知道每当我需要将文本加载到表单中时,我总是可以重置文档正文,但我更愿意理解为什么会这样,如果没有其他的话,那就是为了知识。
非常感谢对此的任何帮助。
I have a WebBrowser
document set to be in edit mode. I am trying to manipulate the inner text of the body element by using WebBrowser.Document.Body.InnerText
, however, WebBrowser.Document.Body
remains null.
Here is the code where I create the document contents:
private WebBrowser HtmlEditor = new WebBrowser();
public HtmlEditControl()
{
InitializeComponent();
HtmlEditor.DocumentText = "<html><body></body></html>";
myDoc = (IHTMLDocument2)HtmlEditor.Document.DomDocument;
myDoc.designMode = "On";
HtmlEditor.Refresh(WebBrowserRefreshOption.Completely);
myContentsChanged = false;
}
I can edit code and everything fine, but I don't understand why HtmlEditor.Document.Body
remains null. I know I could always just reset the document body whenever I need to load text into the form, but I would prefer to understand why this is behaving the way it is, if nothing else then for the knowledge.
Any help on this is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须等待 Web 浏览器的
DocumentCompleted
事件触发,DomDocument.Body
才能不为空。我刚刚对此进行了测试以验证。我想问题仍然存在:当文档尚未完全加载时,如何通过底层 COM 接口进行编辑?我检查了 DocumentCompleted 和构造函数中的
IHTMLDocument2
指针是否相同。它们是,这可能表明底层 COM 对象重用了单个 HTML 文档对象。看来您在构造函数中所做的任何更改至少都有很大的机会被覆盖或引发异常。例如,如果我在构造函数中执行此操作,则会收到错误:
如果我在 DocumentCompleted 处理程序中执行相同操作,则它工作正常。
希望这有帮助。谢谢。
You have to wait for the Web Browser's
DocumentCompleted
event to fire for theDomDocument.Body
to not be null. I just tested this to verify. I suppose the question still remains: how are you able to edit through the underlying COM interface when the document has not completely loaded?I checked to see if the
IHTMLDocument2
pointers were the same in DocumentCompleted and the constructor. They are, which might indicate that the underlying COM object reuses a single HTML document object. It seems like any changes you make in the constructor at least have a pretty good chance of getting overwritten or throwing an exception.For example, if I do this in the constructor, I get an error:
If I do the same in a DocumentCompleted handler, it works fine.
Hope this helps. Thanks.
使用
DocumentCompleted
事件首先发生,当 WebBrowser 控件完成加载文档时发生:或者简单的方法:
Use
DocumentCompleted
event first, it occurs when the WebBrowser control finishes loading a document:or simple way:
您需要让
WebBrowser
控件单独工作一点,以便给它一些时间来设置Document.Body
属性。我通过调用
Application.DoEvents();
来做到这一点。例如在您的代码中:
You need to let the
WebBrowser
control to work alone a bit to give it some time to set theDocument.Body
property.I do that by calling
Application.DoEvents();
.For instance in your code: