3

这就是我所拥有的:

public void initiate(WebBrowser browser)
{         
   browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(refDocumentCompleted);
   // navigate browser to the referal Uri
   browser.Navigate(refreral);

   browser.DocumentCompleted -= refDocumentCompleted;
   //remove here so that it doesn't do this everytime a document is completed, i want it just in this method
}

private void refDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    // want to call navigate method on browser here. but Its out of scope.
}

我想做的是,导航到referral,然后一旦加载导航到另一个页面,该页面存储为类中的全局字符串。

我确定我的麻烦是因为对事件如何工作的理解不足,我试图阅读它,但似乎无法理解它,但我认为我不需要创建自己的处理程序.

4

3 回答 3

2

您已经拥有了 webbrowser 对象。你只需要投射它:

((WebBrowser)sender).Navigate(...);

等等

于 2012-09-07T23:18:55.760 回答
1

使用 lambda,例如:

public void initiate(WebBrowser browser)
{

    browser.DocumentCompleted += (sender, e) => {
        browser.DoStuff(); // it's in scope via closure
    };

    // etc
}

编辑:要添加/删除它,请将 lambda 分配给一个变量:

public void initiate(WebBrowser browser)
{

    var doStuff = (sender, e) => {
        browser.DoStuff(); // it's in scope via closure
    };

    browser.DocumentCompleted += doStuff;

    // etc

    browser.DocumentCompleted -= doStuff;
}
于 2012-09-07T23:20:29.037 回答
0

在您的initiate方法中将其存储WebBrowser在一个实例字段中,然后从您的事件处理程序中引用它:

private WebBrowser _browser;

// inside initiate 
_browser = browser;

// Inside event handler
_browser.Navigate(...);
于 2012-09-07T23:10:23.703 回答