1

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

我刚刚在这里看到了文档

所以我会使用这个方法

我只是创建了一个新类和一个新的 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"); 
4

1 回答 1

2

问题是在控件创建完成之前,您无法设置标题定义。您只需要在设置标题定义时延迟,直到控件准备就绪。我不是 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");
    }
}
于 2012-06-11T15:42:35.570 回答