Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've worked some time ago with the WebControl and like you wanted to load a html from memory but have the same problem, body being null. After some investigation, I noticed that the Navigate and NavigateToString methods work asynchronously, so it needs a little time for the control to load the document, the document is not available right after the call to Navigate. So i did something like (wbChat is the WebBrowser control):</p> <pre><code>wbChat.NavigateToString("&lt;html&gt;&lt;body&gt;&lt;div&gt;first line&lt;/div&gt;&lt;/body&gt;&lt;html&gt;"); DoEvents(); </code></pre> <p>where DoEvents() is implemeted as:</p> <pre><code>[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)] public void DoEvents() { DispatcherFrame frame = new DispatcherFrame(); Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrame), frame); Dispatcher.PushFrame(frame); } </code></pre> <p>and it worked for me, after the DoEvents call, I could obtain a non-null body:</p> <pre><code>mshtml.IHTMLDocument2 doc2 = (mshtml.IHTMLDocument2)wbChat.Document; mshtml.HTMLDivElement div = (mshtml.HTMLDivElement)doc2.createElement("div"); div.innerHTML = "some text"; mshtml.HTMLBodyClass body = (mshtml.HTMLBodyClass)doc2.body; if (body != null) { body.appendChild((mshtml.IHTMLDOMNode)div); body.scrollTop = body.scrollHeight; } else Console.WriteLine("body is still null"); </code></pre> <p>I don't know if this is the <strong>right</strong> way of doing this, but it fixed the problem for me, maybe it helps you too.</p> <p>Later Edit:</p> <pre><code>public object ExitFrame(object f) { ((DispatcherFrame)f).Continue = false; return null; } </code></pre> <p>The DoEvents method is necessary on WPF. For System.Windows.Forms one can use Application.DoEvents().</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload