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 が返されます。