网络浏览器不加载文档
Posted
技术标签:
【中文标题】网络浏览器不加载文档【英文标题】:webbrowser not loading document 【发布时间】:2009-05-04 18:01:08 【问题描述】:我正在尝试将 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>\0", however.
// This line setting the title throws a HRESULT COM error.
webBrowser1.Document.Title = "foobar!";
wb_c 事件处理程序也永远不会被调用。 webbrowser 控件被定义为窗体上的控件。表单本身仅由浏览器和按钮组成。
有人有什么想法吗?我以前用过这个类没有任何问题,但是这次.Net大神否认我!我的最终目标是打印呈现的文档,但现在我什至无法让它接受我的 HTML。也许我需要一些圣水之类的。
编辑:如果上面的标题行被删除,wb_c 事件处理程序永远不会被触发。就好像 COM 组件本身有问题一样。
编辑 2:根据大众需求,这是我的完整代码。
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;
这是一个在 VS 2005 中运行的 .Net 2.0 项目。System.Windows.Forms.dll 是 v2.0.50727。
编辑 3:将此行添加到 Form2 构造函数的末尾:
webBrowser1.Navigate("about:blank");
是否触发事件处理程序,但在设置文档文本时不会影响代码的行为。在 webBrowser1.Document.Text 行之后设置断点仍然给出相同的“\0”字符串,并且尝试设置标题仍然给出 COM HERROR。
【问题讨论】:
我刚刚尝试了你的示例,不管你信不信,你的事件处理程序正在被调用。如果您在引发异常的代码上放置断点,它将命中。尝试取出 throw 异常,并将其替换为 webBrowser1.Document.Title = "foobar!"; 【参考方案1】:在您可以操作文档之前,您需要执行导航命令。要使用 WebBrowser 从头开始构建 HTML 页面,只需导航到 "about:blank",如下所示:
WebBrowser browser = new WebBrowser();
browser.Navigate("about:blank");
browser.Document.Write(html);
然后在根元素上使用 InnerHtml 而不是 DocumentText 属性来应用 Html。
【讨论】:
这是正确答案。不知道为什么其他答案没有解决基本问题而获得投票?【参考方案2】:尝试移动线路:
webBrowser1.Document.Title = "foobar!";
进入你的 wb_c 方法。我认为问题在于,当您调用它时, Document 属性尚未完全设置,并且您会得到一个空引用异常。如果你等到页面加载完毕,你应该没问题。
更新:尝试了您的示例,并且您的事件处理程序被调用,但是我怀疑它是从另一个线程调用的。因此,它到达抛出异常的行,但您实际上从未看到它,因为它在另一个线程中。取出抛出异常的那一行,换成:
webBrowser1.Document.Title = "foobar!";
这应该可以解决问题。
【讨论】:
wb_c 处理程序永远不会被触发,即使标题行被删除。 这很奇怪,我使用您的代码创建了一个简单的测试应用程序,处理程序被调用并设置 DocumentTitle 没有问题。 这很奇怪。我得到与 BFree 相同的结果。也许我们需要看到更多的代码。 我怀疑异常被浏览器控件吞没了。它不在不同的线程上,它只是捕获所有异常(可能拼命试图控制浏览器 COM 控件导致问题的趋势)。【参考方案3】:文档加载是异步的,因此在您设置标题时,无法保证文档已实际加载。在尝试更改文档之前,您需要处理适当的浏览器事件以检测导航何时完成。
更新
在我使用浏览器的所有情况下,我必须先导航到about:blank
页面,然后才能修改文档。也许您应该在设置DocumentText
之前尝试一下。
【讨论】:
永远不会触发 DocumentCompleted 事件。【参考方案4】:我使用上面使用 about:blank 的方法,它工作正常!我昨天发表了一篇关于这种方法的文章,今天我刚刚在这里找到了这个主题:) 我的文章在这里:http://starikovs.com/2009/11/25/set-html-webbrowser-csharp/
【讨论】:
以上是关于网络浏览器不加载文档的主要内容,如果未能解决你的问题,请参考以下文章