WebBrowser.Document.Body 始终为 null

发布于 2024-12-15 16:56:02 字数 740 浏览 3 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(4

春花秋月 2024-12-22 16:56:02

您必须等待 Web 浏览器的 DocumentCompleted 事件触发,DomDocument.Body 才能不为空。我刚刚对此进行了测试以验证。我想问题仍然存在:当文档尚未完全加载时,如何通过底层 COM 接口进行编辑?

我检查了 DocumentCompleted 和构造函数中的 IHTMLDocument2 指针是否相同。它们是,这可能表明底层 COM 对象重用了单个 HTML 文档对象。看来您在构造函数中所做的任何更改至少都有很大的机会被覆盖或引发异常。

例如,如果我在构造函数中执行此操作,则会收到错误:

IHTMLDocument2 p1 = (IHTMLDocument2) HTMLEditor.Document.DomDocument;

p1.title = "Hello world!";

如果我在 DocumentCompleted 处理程序中执行相同操作,则它工作正常。

希望这有帮助。谢谢。

You have to wait for the Web Browser's DocumentCompleted event to fire for the DomDocument.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:

IHTMLDocument2 p1 = (IHTMLDocument2) HTMLEditor.Document.DomDocument;

p1.title = "Hello world!";

If I do the same in a DocumentCompleted handler, it works fine.

Hope this helps. Thanks.

旧瑾黎汐 2024-12-22 16:56:02

使用 DocumentCompleted 事件首先发生,当 WebBrowser 控件完成加载文档时发生:

public HtmlEditControl()
{
    InitializeComponent();
    HtmlEditor.DocumentText = "<html><body></body></html>";
    HtmlEditor.DocumentCompleted += HtmlEditorDocumentCompleted;
}

void HtmlEditorDocumentCompleted(object sender, 
                                 WebBrowserDocumentCompletedEventArgs e)
{
    myDoc = (IHTMLDocument2)((WebBrowser)sender).Document.DomDocument;
    myDoc.designMode = "On";
    HtmlEditor.Refresh(WebBrowserRefreshOption.Completely);
    myContentsChanged = false;
}

或者简单的方法:

public HtmlEditControl()
{
    InitializeComponent();
    HtmlEditor.DocumentText = "<html><body></body></html>";
    HtmlEditor.DocumentCompleted += (sender, e) =>
            {
                myDoc = (IHTMLDocument2) HtmlEditor.Document.DomDocument;
                myDoc.designMode = "On";
                HtmlEditor.Refresh(WebBrowserRefreshOption.Completely);
                myContentsChanged = false;
            };
}

Use DocumentCompleted event first, it occurs when the WebBrowser control finishes loading a document:

public HtmlEditControl()
{
    InitializeComponent();
    HtmlEditor.DocumentText = "<html><body></body></html>";
    HtmlEditor.DocumentCompleted += HtmlEditorDocumentCompleted;
}

void HtmlEditorDocumentCompleted(object sender, 
                                 WebBrowserDocumentCompletedEventArgs e)
{
    myDoc = (IHTMLDocument2)((WebBrowser)sender).Document.DomDocument;
    myDoc.designMode = "On";
    HtmlEditor.Refresh(WebBrowserRefreshOption.Completely);
    myContentsChanged = false;
}

or simple way:

public HtmlEditControl()
{
    InitializeComponent();
    HtmlEditor.DocumentText = "<html><body></body></html>";
    HtmlEditor.DocumentCompleted += (sender, e) =>
            {
                myDoc = (IHTMLDocument2) HtmlEditor.Document.DomDocument;
                myDoc.designMode = "On";
                HtmlEditor.Refresh(WebBrowserRefreshOption.Completely);
                myContentsChanged = false;
            };
}
千紇 2024-12-22 16:56:02

您需要让 WebBrowser 控件单独工作一点,以便给它一些时间来设置 Document.Body 属性。

我通过调用 Application.DoEvents(); 来做到这一点。

例如在您的代码中:

private WebBrowser HtmlEditor = new WebBrowser();
public HtmlEditControl()
{
    InitializeComponent();
    HtmlEditor.DocumentText = "<html><body></body></html>";

    // Let's leave the WebBrowser control working alone.
    while (HtmlEditor.Document.Body == null)
    {
        Application.DoEvents();
    }

    myDoc = (IHTMLDocument2)HtmlEditor.Document.DomDocument;
    myDoc.designMode = "On";
    HtmlEditor.Refresh(WebBrowserRefreshOption.Completely);
    myContentsChanged = false;
}

You need to let the WebBrowser control to work alone a bit to give it some time to set the Document.Body property.

I do that by calling Application.DoEvents();.

For instance in your code:

private WebBrowser HtmlEditor = new WebBrowser();
public HtmlEditControl()
{
    InitializeComponent();
    HtmlEditor.DocumentText = "<html><body></body></html>";

    // Let's leave the WebBrowser control working alone.
    while (HtmlEditor.Document.Body == null)
    {
        Application.DoEvents();
    }

    myDoc = (IHTMLDocument2)HtmlEditor.Document.DomDocument;
    myDoc.designMode = "On";
    HtmlEditor.Refresh(WebBrowserRefreshOption.Completely);
    myContentsChanged = false;
}
绝對不後悔。 2024-12-22 16:56:02
if (HtmlEditor.Document.Body == null)
{
   HtmlEditor.Document.OpenNew(false).Write(@"<html><body><div id=""editable""></div></body></html>");
}
HtmlEditor.Document.Body.SetAttribute("contentEditable", "true");
if (HtmlEditor.Document.Body == null)
{
   HtmlEditor.Document.OpenNew(false).Write(@"<html><body><div id=""editable""></div></body></html>");
}
HtmlEditor.Document.Body.SetAttribute("contentEditable", "true");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文