Использование STA для управления WebBrowser - PullRequest
1 голос
/ 30 августа 2011

Я пытаюсь добавить веб-браузер в приложение, чтобы иметь возможность отображать веб-страницу, но по-прежнему возникают проблемы с типом потоков.

В настоящее время я получаю:

Элемент управления ActiveX '8856f961-340a-11d0-a96b-00c04fd705a2' не может быть создается, потому что текущий поток не является однопоточным квартира.

И я нашел этот вопрос, который относится к этой проблеме: Однопоточная квартира - не может создать экземпляр элемента управления ActiveX

Однако в моем коде нет метода Main (), и когда я пытаюсь решить этот вопрос, при добавлении WebBrowser к элементам управления Form я сталкиваюсь с ошибкой.


Я использую код, найденный здесь:

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

    }
}
...