在winformc#中启用控件的WebControl问题

本文关键字:WebControl 问题 控件 启用 winformc# | 更新日期: 2023-09-27 18:15:54

我会在我的Winform应用程序c#中使用这个浏览器框架。

I just Saw the Documentation HERE

所以我会使用这个方法

我只是创建了一个新的类和一个新的Awesomium.Windows.Forms.WebControl对象。

现在,如果我使用它没有任何特殊的方法(只是那些创建对象和加载Url源它的工作。但是当我想使用这个方法时:

browser.SetHeaderDefinition("MyHeader", myCol); //myCol is a NameValueCollection 

我收到这个错误The control is disabled either manually or it has been destroyed.

在我链接的第一页上写着:


除了它的常规含义,Enabled属性在WebControl中还有一个特殊的含义:它还指示底层视图是否有效和启用。

当WebControl被销毁(通过调用Close()或Shutdown())或从未被正确实例化时,它被认为是无效的。

手动将Enabled属性设置为true,将暂时使控件禁用。……

当被禁用时(因为视图被销毁或者因为你手动设置了这个属性),试图访问这个控件的成员,可能会导致InvalidOperationException(参见每个成员的文档)。

现在我试着玩ENABLED属性,但我仍然得到这个错误。我该怎么做才能解决这个问题?我真的不明白。

   Awesomium.Windows.Forms.WebControl browser = 
                                        new Awesomium.Windows.Forms.WebControl();
                    this.SuspendLayout();
        browser.Location = new System.Drawing.Point(1, 12);
       browser.Name = "webControl1";
       browser.Size = new System.Drawing.Size(624, 442);
     browser.Source = new System.Uri("http://www.google.it", System.UriKind.Absolute);
                    browser.TabIndex = 0;
**** This below is the code that i cant use cause i get the error control
// System.Collections.Specialized.NameValueCollection myCol = 
// new System.Collections.Specialized.NameValueCollection();
//            myCol.Add("Referer", "http://www.yahoo.com");
//            browser.SetHeaderDefinition("MyHeader", myCol);
//            browser.AddHeaderRewriteRule("http://*", "MyHeader"); 

在winformc#中启用控件的WebControl问题

问题是在控件创建完成之前无法设置标题定义。您只需要在设置标题定义时延迟,直到控件准备好。我不是Winforms专家,所以可能有一个更好的事件来确定控件在其生命周期中的位置,但这里有一个你发布的工作修改,只是使用控件的Paint事件来推迟有问题的方法调用:

public partial class Form1 : Form
{
    private Awesomium.Windows.Forms.WebControl browser;
    public Form1()
    {
        InitializeComponent();
        browser = new Awesomium.Windows.Forms.WebControl();
        //delay until control is ready
        browser.Paint += browser_Paint;
        Controls.Add(browser);
        browser.Location = new System.Drawing.Point(1, 12);
        browser.Name = "webControl1";
        browser.Size = new System.Drawing.Size(624, 442);
        browser.Source = new System.Uri("http://www.google.it", System.UriKind.Absolute);
        browser.TabIndex = 0;
    }
    void browser_Paint(object sender, PaintEventArgs e)
    {
        browser.Paint -= browser_Paint;
        System.Collections.Specialized.NameValueCollection myCol =
            new System.Collections.Specialized.NameValueCollection();
        myCol.Add("Referer", "http://www.yahoo.com");
        browser.SetHeaderDefinition("MyHeader", myCol);
        browser.AddHeaderRewriteRule("http://*", "MyHeader");
    }
}