2016-08-02 36 views
0

因此,我在發送到Url後在瀏覽器控件的文檔上獲得了null引用。我認爲這是因爲文檔沒有完成加載。所以我增加了一個事件處理程序:C#執行瀏覽器控件完成加載後的執行方法?

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

會有人願意告訴我怎麼去呢?

編輯:

我還要補充一點,這種方法可行,如果正確,我已經在頁面上。當我導航到頁面然後嘗試該方法時,問題就出現了。

回答

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]); 
    } 
相關問題