如何在 C# 中使用 WebBrowser 控件 DocumentCompleted 事件? [英] How to use WebBrowser control DocumentCompleted event in C#?

查看:25
本文介绍了如何在 C# 中使用 WebBrowser 控件 DocumentCompleted 事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开始写这个问题之前,我试图解决以下问题

Before starting writing this question, i was trying to solve following

// 1. navigate to page
// 2. wait until page is downloaded
// 3. read and write some data from/to iframe 
// 4. submit (post) form

问题是,如果网页上存在 iframe,则 DocumentCompleted 事件将被触发多次(在每个文档完成后).程序很可能会尝试从未完成的 DOM 中读取数据,并且自然会失败.

The problem was, that if a iframe exists on a web page, DocumentCompleted event would get fired more then once (after each document has been completed). It was highly likely that program would have tried to read data from DOM that was not completed and naturally - fail.

但是突然在写这个问题时'如果'怪物会怎样启发了我,我解决了我试图解决的问题.由于我在谷歌上失败了,我想把它贴在这里会很好.

But suddenly while writing this question 'What if' monster inspired me, and i fix'ed the problem, that i was trying to solve. As i failed Google'ing this, i thought it would be nice to post it here.

    private int iframe_counter = 1; // needs to be 1, to pass DCF test
    public bool isLazyMan = default(bool);

    /// <summary>
    /// LOCK to stop inspecting DOM before DCF
    /// </summary>
    public void waitPolice() {
        while (isLazyMan) Application.DoEvents();
    }

    private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e) {
        if(!e.TargetFrameName.Equals(""))
            iframe_counter --;
        isLazyMan = true;
    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
        if (!((WebBrowser)sender).Document.Url.Equals(e.Url))
            iframe_counter++;
        if (((WebBrowser)sender).Document.Window.Frames.Count <= iframe_counter) {//DCF test
            DocumentCompletedFully((WebBrowser)sender,e);
            isLazyMan = false; 
        }
    }

    private void DocumentCompletedFully(WebBrowser sender, WebBrowserDocumentCompletedEventArgs e){
        //code here
    }

至少现在,我的 5m hack 似乎运行良好.

For now at least, my 5m hack seems to be working fine.

也许我真的无法查询 google 或 MSDN,但我找不到:如何在 C# 中使用 webbrowser 控件 DocumentCompleted 事件?"

Maybe i am really failing at querying google or MSDN, but i can not find: "How to use webbrowser control DocumentCompleted event in C# ?"

备注: 在学习了很多关于 webcontrol 的知识后,我发现它可以做一些有趣的事情.

Remark: After learning a lot about webcontrol, I found that it does FuNKY stuff.

即使你检测到文档已经完成,在大多数情况下它也不会永远保持这样.页面更新可以通过多种方式完成 - 帧刷新、ajax 之类的请求或服务器端推送(您需要一些支持异步通信并具有 html 或 JavaScript 互操作的控件).还有一些 iframe 永远不会加载,所以永远等待它们并不是最好的主意.

Even if you detect that the document has completed, in most cases it wont stay like that forever. Page update can be done in several ways - frame refresh, ajax like request or server side push (you need to have some control that supports asynchronous communication and has html or JavaScript interop). Also some iframes will never load, so it's not best idea to wait for them forever.

我最终使用了:

if (e.Url != wb.Url)

推荐答案

您可能还想了解 AJAX 调用.

You might want to know the AJAX calls as well.

考虑使用这个:

private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    string url = e.Url.ToString();
    if (!(url.StartsWith("http://") || url.StartsWith("https://")))
    {
            // in AJAX
    }

    if (e.Url.AbsolutePath != this.webBrowser.Url.AbsolutePath)
    {
            // IFRAME 
    }
    else
    {
            // REAL DOCUMENT COMPLETE
    }
}

这篇关于如何在 C# 中使用 WebBrowser 控件 DocumentCompleted 事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆