gpt4 book ai didi

c# - 网络浏览器未加载文档

转载 作者:太空狗 更新时间:2023-10-30 00:29:17 25 4
gpt4 key购买 nike

我正在尝试将 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 事件处理程序也从未被调用。 Web 浏览器控件被定义为窗体上的控件。表单本身仅包含浏览器和按钮。

有人有什么想法吗?我以前用过这个类,没有任何问题,但这次.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。

最佳答案

在您可以操作文档之前,您需要执行导航命令。要使用 WebBrowser 从头开始​​构建 HTML 页面,只需导航到 “about:blank”,如下所示:

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

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

关于c# - 网络浏览器未加载文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/821207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com