浏览器控制完成加载后的C#执行方法?

时间:2016-08-02 21:06:47

标签: c# webbrowser-control

因此,我在发送到Url后获得了浏览器控件文档的空引用。我以为是因为文件没有完成加载。所以我添加了一个事件处理程序:

    string[] m_ArgCache = null;
    internal void AutomateThreadCreation(string title, string content)
    {
        SendToNewThreadByIndex();

        m_ArgCache = new string[] { title, content };
        Browser.DocumentCompleted += Browser_DocumentCompleted;
    }

    void Browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if(m_ArgCache != null)
            AttemptPost(m_ArgCache[0], m_ArgCache[1]);
    }

但是,我仍然收到一个空错误:

enter image description here

有人愿意告诉我该怎么做吗?

编辑:

我还应该补充一点,如果我已经在页面上,这种方法可以正常工作。当我导航到页面然后尝试该方法时会出现问题。

1 个答案:

答案 0 :(得分:0)

解决方案是为Browser.Validated而不是DocumentCompleted创建一个事件处理程序。

    string[] m_ArgCache = null;
    internal void AutomateThreadCreation(string title, string content)
    {
        SendToNewThreadByIndex();

        m_ArgCache = new string[] { title, content };
        Browser.Validated += Browser_Validated;
    }

    void Browser_Validated(object sender, EventArgs e)
    {
        if (m_ArgCache != null)
            AttemptPost(m_ArgCache[0], m_ArgCache[1]);
    }
相关问题