我正在尝试将 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:根据大众的需求,这是我的代码的完整 blob。
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。