使用 STA 进行 Web 浏览器控制
我正在尝试将 WebBrowser 添加到应用程序以便能够显示网页,但始终遇到线程类型问题。
目前我得到:
ActiveX 控件“8856f961-340a-11d0-a96b-00c04fd705a2”不能 实例化,因为当前线程不是单线程 公寓。
我发现了与此问题相关的问题: 单线程公寓 - 不能实例化 ActiveX 控件
但是,我的代码中没有 Main() 方法,当我尝试解决该问题时,在将 WebBrowser 添加到表单控件时遇到错误。
我正在使用此处找到的代码:
http://www.apress.com/9781430228530
直接下载: http://www.apress.com/downloadable/download/sample/sample_id/1173/
我在第 14 章,在 HorizontalExplorerBar 文件夹内。
这是我用来参考的代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using ProIeDev.BandObjectDemo.BandObject;
using System.Threading;
using System.Windows.Forms;
namespace ProIeDev.BandObjectDemo.HorizontalExplorerBar
{
[ComVisible(true)]
public class HorizontalExplorerBar : BandObject.BandObject
{
private WebBrowser webBrowser1;
private System.Windows.Forms.RadioButton radioButton1;
public HorizontalExplorerBar()
{
// var t = new Thread(InitializeComponent);
// t.SetApartmentState(ApartmentState.STA);
// t.Start();
InitializeComponent();
}
public override void DefineMetadata()
{
ClassId = "{B028EA5C-B226-4E4B-88C6-8842A152BC5B}";
ProgId = "ProIeDev.BandObjectDemo.HorizontalExplorerBar";
ObjectName = "TodoBar Horizontal";
ObjectTitle = "TodoBar Horizontal";
HelpText = "TodoBar Horizontal";
Style = BandObjectTypes.HorizontalExplorerBar;
}
#region Form Designer
private void InitializeComponent()
{
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.SuspendLayout();
//
// radioButton1
//
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(323, 34);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(85, 17);
this.radioButton1.TabIndex = 0;
this.radioButton1.TabStop = true;
this.radioButton1.Text = "radioButton1";
this.radioButton1.UseVisualStyleBackColor = true;
//
// webBrowser1
//
this.webBrowser1.Location = new System.Drawing.Point(44, 19);
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.Size = new System.Drawing.Size(250, 250);
this.webBrowser1.TabIndex = 1;
//
// HorizontalExplorerBar
//
this.BackColor = System.Drawing.Color.DarkBlue;
this.Controls.Add(this.webBrowser1);
this.Controls.Add(this.radioButton1);
this.Name = "HorizontalExplorerBar";
this.Size = new System.Drawing.Size(456, 177);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion Form Designer
}
}
I am trying to add a WebBrowser to an application to be able to display a webpage, but keep getting a problem with the threading type.
Currently I am getting:
ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be
instantiated because the current thread is not in a single-threaded
apartment.
And I have found this question that relates to this problem: Single-threaded apartment - cannot instantiate ActiveX control
However, I do not have a Main() method in my code and when I try the solution to that question, then I encounter an error when adding the WebBrowser to the Form controls.
I am using the code found here:
http://www.apress.com/9781430228530
Direct Download: http://www.apress.com/downloadable/download/sample/sample_id/1173/
And I am on Chapter 14 and inside the HorizontalExplorerBar folder.
Here is the code that I am using for reference:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using ProIeDev.BandObjectDemo.BandObject;
using System.Threading;
using System.Windows.Forms;
namespace ProIeDev.BandObjectDemo.HorizontalExplorerBar
{
[ComVisible(true)]
public class HorizontalExplorerBar : BandObject.BandObject
{
private WebBrowser webBrowser1;
private System.Windows.Forms.RadioButton radioButton1;
public HorizontalExplorerBar()
{
// var t = new Thread(InitializeComponent);
// t.SetApartmentState(ApartmentState.STA);
// t.Start();
InitializeComponent();
}
public override void DefineMetadata()
{
ClassId = "{B028EA5C-B226-4E4B-88C6-8842A152BC5B}";
ProgId = "ProIeDev.BandObjectDemo.HorizontalExplorerBar";
ObjectName = "TodoBar Horizontal";
ObjectTitle = "TodoBar Horizontal";
HelpText = "TodoBar Horizontal";
Style = BandObjectTypes.HorizontalExplorerBar;
}
#region Form Designer
private void InitializeComponent()
{
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.SuspendLayout();
//
// radioButton1
//
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(323, 34);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(85, 17);
this.radioButton1.TabIndex = 0;
this.radioButton1.TabStop = true;
this.radioButton1.Text = "radioButton1";
this.radioButton1.UseVisualStyleBackColor = true;
//
// webBrowser1
//
this.webBrowser1.Location = new System.Drawing.Point(44, 19);
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.Size = new System.Drawing.Size(250, 250);
this.webBrowser1.TabIndex = 1;
//
// HorizontalExplorerBar
//
this.BackColor = System.Drawing.Color.DarkBlue;
this.Controls.Add(this.webBrowser1);
this.Controls.Add(this.radioButton1);
this.Name = "HorizontalExplorerBar";
this.Size = new System.Drawing.Size(456, 177);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion Form Designer
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论