想在geckowebbrowser上多次检测到文档已完成

问题描述 投票:0回答:1

我正在使用geckowebbrowser在C#上使用webbrowser应用程序。在这里,我给出一个URL来导航,而不是检测完成的文档。在此之后,我在文本框中输入一些值,然后单击按钮。现在继承人是我的问题。在点击按钮后,我再次想要检测documentcompleted事件并做一些事情。现在,我如何使用单个事件和单个事件处理程序执行不同的任务。

        geckoWebBrowser1.Navigate("www.facebook.com");
        geckoWebBrowser1.DocumentCompleted += new   EventHandler<Gecko.Events.GeckoDocumentCompletedEventArgs>(geckoWebBrowser1_DocumentCompleted);

        GeckoHtmlElement ge= (GeckoHtmlElement)geckoWebBrowser1.Document.GetElementById("u_0_5");
        ge.Click();

此时我想检测documentcompleted事件并执行其他一些任务。这该怎么做?提前致谢。

c# webbrowser-control domdocument gecko
1个回答
0
投票

只需将全局变量添加到您的类:

CurrentAction MyAction = CurrentAction.UNKNOWN;

public enum CurrentAction
{
    UNKNOWN = -1,
    ClickButtonOne,
    DoSomethingElse,
    ClickTheOtherButton,
    DoNothing,
}

然后在导航到第一页之前,设置此标志:

MyAction = CurrentAction.ClickButtonOne;
geckoWebBroser1.Navigate("site.com");

DocumentCompleted事件中这样做:

switch (MyAction)
{
    case ClickButtonOne:
        // find the button, check if not null
        MyAction = CurrentAction.DoSomethingElse; // set a new action
        button.Click();
        break;
    case DoSomethingElse:
        // Do something
        MyAction = CurrentAction.ClickTheOtherButton; // set a new action
        break;
    case ClickTheOtherButton:
        // this is the last page, so lets stop here
        MyAction = CurrentAction.DoNothing; // specific action
        break;           
    default:
        Debug.Assert(false); break;
}

因此,每次进入DocumentCompleted时,代码都会执行不同的任务。只是不要忘记设置你需要的动作,并进行空检查。另外我建议你实施NavigationError事件,这样你就会知道出了什么问题。

© www.soinside.com 2019 - 2024. All rights reserved.