webbrowser没有加载文件

Amy*_*Amy 4 c# webbrowser-control

我正在尝试将一个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!";
}
Run Code Online (Sandbox Code Playgroud)

永远不会调用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;
}
Run Code Online (Sandbox Code Playgroud)

这是在VS 2005中运行的.Net 2.0项目.System.Windows.Forms.dll是v2.0.50727.

编辑3:将此行添加到Form2构造函数的末尾:

webBrowser1.Navigate("about:blank");
Run Code Online (Sandbox Code Playgroud)

是否触发事件处理程序,但它设置文档文本时并不影响代码的行为.在webBrowser1.Document.Text行之后设置断点仍然提供相同的"\ 0"字符串,并且尝试设置标题仍然给出了COM HERROR.

Rob*_*ert 6

在您可以操作Document之前,您需要执行navigate命令.要使用WebBrowser从头构建HTML页面,只需导航到"about:blank",如下所示:

WebBrowser browser = new WebBrowser();
browser.Navigate("about:blank");
browser.Document.Write(html);
Run Code Online (Sandbox Code Playgroud)

然后使用根元素上的InnerHtml而不是DocumentText属性来按原样应用Html.