C# 浏览器未加载文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/821207/
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
webbrowser not loading document
提问by Amy
I'm trying to load an html document into a WebBrowser control, but I'm at my wits end. Here's a sample:
我正在尝试将 html 文档加载到 WebBrowser 控件中,但我无能为力。这是一个示例:
public void Button_Click(object sender, EventArgs e)
{
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_c);
webBrowser1.DocumentText = "<html>foo</html>";
// The documenttext property is NOT what was set above.
// No exception is thrown. It's always "<html></html>public partial class Form2 : Form
{
[STAThread]
public static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form2());
}
public Form2()
{
InitializeComponent();
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_c);
}
void wb_c(object sender, WebBrowserDocumentCompletedEventArgs e)
{
throw new Exception("The method or operation is not implemented.");
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.DocumentText = "<html>foo</html>";
}
}
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// webBrowser1
//
this.webBrowser1.Location = new System.Drawing.Point(12, 12);
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.Size = new System.Drawing.Size(117, 99);
this.webBrowser1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(90, 165);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Controls.Add(this.webBrowser1);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.WebBrowser webBrowser1;
private System.Windows.Forms.Button button1;
}
", however.
// This line setting the title throws a HRESULT COM error.
webBrowser1.Document.Title = "foobar!";
}
The wb_c event handler is never called, either. The webbrowser control is defined as a control on the form. The form itself consists of only the browser and the button.
wb_c 事件处理程序也永远不会被调用。webbrowser 控件被定义为表单上的控件。表单本身只包含浏览器和按钮。
Does anyone have any ideas? I've used this class before without any issues, but this time the .Net gods are denying me! My end goal is to print the rendered document, but right now I can't even get it to accept my HTML. Maybe I need some holy water or something.
有没有人有任何想法?我以前使用过这个类没有任何问题,但这次 .Net 众神否认了我!我的最终目标是打印呈现的文档,但现在我什至无法让它接受我的 HTML。也许我需要一些圣水什么的。
Edit: If the Title line is removed above, the wb_c event handler is never getting triggered. It's as though there's something wrong with the COM component itself, or something.
编辑:如果上面的标题行被删除,则 wb_c 事件处理程序永远不会被触发。就好像 COM 组件本身有问题,或者什么。
Edit 2: By popular demand, here is a complete blob of my code.
编辑 2:根据大众需求,这里是我的代码的完整 blob。
webBrowser1.Navigate("about:blank");
This is a .Net 2.0 project running in VS 2005. System.Windows.Forms.dll is v2.0.50727.
这是一个在 VS 2005 中运行的 .Net 2.0 项目。System.Windows.Forms.dll 是 v2.0.50727。
EDIT 3: Adding this line to the end of the Form2 constructor:
编辑 3:将此行添加到 Form2 构造函数的末尾:
webBrowser1.Document.Title = "foobar!";
Doestrigger the event handler, but it doesn't otherwise affect the behavior of the code when setting the document text. Setting a breakpoint after the webBrowser1.Document.Text line still gives the same "\0" string, and trying to set the title still gives a COM HERROR.
是否触发事件处理程序,但它设置文档文本时并不影响代码的行为。在 webBrowser1.Document.Text 行之后设置断点仍然给出相同的“\0”字符串,并且尝试设置标题仍然给出 COM HERROR。
采纳答案by BFree
Try moving the line:
尝试移动该行:
webBrowser1.Document.Title = "foobar!";
into your wb_c method. I think the problem is that when you're calling it, the Document property isn't completley set yet, and you're getting a null reference exception. If you wait till the page is loaded, you should be ok.
进入你的 wb_c 方法。我认为问题在于,当您调用它时, Document 属性尚未完全设置,并且您收到空引用异常。如果你等到页面加载,你应该没问题。
UPDATE:Tried your sample, and your event handler IS getting called, however I suspect it's being called from another thread. Therefore, it gets to the line where the Exception is thrown, but you're never actually seeing it, because it's in another thread. Take out the line that throws the exception, and replace it with:
更新:尝试了您的示例,并且正在调用您的事件处理程序,但是我怀疑它是从另一个线程调用的。因此,它会到达抛出异常的那一行,但您实际上从未看到它,因为它在另一个线程中。取出抛出异常的那一行,替换为:
WebBrowser browser = new WebBrowser();
browser.Navigate("about:blank");
browser.Document.Write(html);
That should do the trick.
这应该够了吧。
回答by Jeff Yates
Document loading is asynchronous so by the time you set the Title, there is no guarantee that the document has actually loaded. You need to handle the appropriate browser events to detect when navigation is complete before trying to alter the document.
文档加载是异步的,因此在您设置标题时,无法保证文档已实际加载。在尝试更改文档之前,您需要处理适当的浏览器事件以检测导航何时完成。
Update
更新
In all situations where I've used the browser, I've had to navigate to the about:blank
page first before I could modify the document. Perhaps you should try this before setting the DocumentText
.
在我使用浏览器的所有情况下,我都必须先导航到about:blank
页面,然后才能修改文档。也许您应该在设置DocumentText
.
回答by Robert
Before you can manipulate the Document you need to execute the navigate command. To use the WebBrowser to construct a HTML page from scratch just navigate to "about:blank"like so:
在您可以操作 Document 之前,您需要执行导航命令。要使用 WebBrowser 从头开始构建 HTML 页面,只需导航到“about:blank”,如下所示:
##代码##Then use the InnerHtml on the root element and not the DocumentText property to apply Html as you are.
然后在根元素上使用 InnerHtml 而不是 DocumentText 属性来按原样应用 Html。
回答by starikovs
I use the method above that uses about:blank and it works ok! I published an article about this method yesterday and today i've just found this topic here :) My article here: http://starikovs.com/2009/11/25/set-html-webbrowser-csharp/
我使用上面使用 about:blank 的方法,它工作正常!我昨天发表了一篇关于这种方法的文章,今天我刚刚在这里找到了这个话题:) 我的文章在这里:http: //starikovs.com/2009/11/25/set-html-webbrowser-csharp/