我正在尝试将一个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。

有帮助吗?

解决方案

尝试移动线:

webBrowser1.Document.Title = "foobar!";

进入你的wb_c方法。我认为问题在于,当你调用它时,Document属性还没有完成设置,并且你得到一个空引用异常。如果你等到页面加载,你应该没问题。

更新:尝试了您的示例,并且您的事件处理程序被调用,但我怀疑它是从另一个线程调用的。因此,它会到达抛出Exception的行,但是你实际上从未看到它,因为它在另一个线程中。取出抛出异常的行,并将其替换为:

<*>

这应该可以解决问题。

其他提示

在操作Document之前,您需要执行navigate命令。要使用WebBrowser从头开始构建HTML页面,只需导航到&quot; about:blank&quot; ,如下所示:

WebBrowser browser = new WebBrowser();
browser.Navigate("about:blank");
browser.Document.Write(html);

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

文档加载是异步的,因此在设置标题时,无法保证文档实际已加载。在尝试更改文档之前,您需要处理相应的浏览器事件以检测导航何时完成。

更新

在我使用浏览器的所有情况下,在修改文档之前,我必须首先导航到 about:blank 页面。也许你应该在设置 DocumentText 之前尝试这个。

我使用上面使用about:blank的方法,它可以正常工作!我昨天发表了一篇关于这种方法的文章,今天我刚刚在这里发现了这个话题:) 我的文章: http://starikovs.com/2009/11 / 25 /设定HTML的网页浏览器-CSHARP /

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top