WPF 网页浏览器的 LoadCompleted 事件 [英] WPF webbrowser's LoadCompleted event

查看:75
本文介绍了WPF 网页浏览器的 LoadCompleted 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF 网络浏览器的 LoadCompleted 事件何时触发?此事件是否等待 aspx 页面中的任何 ajax 调用完成.

When will the WPF webbrowser's LoadCompleted event fires? Is this event waits for any ajax calls to complete in the aspx page.

我有一个 wpf 应用程序,其中一个 webbrowser 控件放置在窗口窗体上,并使用 Navigate 方法加载了一个网页.即使某些 ajax 调用未初始化/等待,LoadCompleted 事件也会触发.

i have a wpf app in which a webbrowser control placed on a window form and a webpage loaded using Navigate method. The LoadCompleted event fires even when some ajax calls not initialized/waiting.

请建议在 100% 加载网页后触发的任何事件,包括所有 ajax 调用.

Please suggest any event which fires after loading the webpage 100% including all ajax calls.

推荐答案

我已经解决了这个问题.

I have been able to solve such issue.

您将需要一些 3rd 方程序集:

You will need some 3rd party assemblies :

  1. FiddlerCore:将充当 http 代理,嵌入您的应用程序中
  2. Awesomium.net:一个 WebBrowser 控件,与 Chromium 引擎配合使用.我选择这个引擎,因为它允许为应用程序指定一个代理服务器.
  1. FiddlerCore: will act as a http proxy, embedded in your application
  2. Awesomium.net: a WebBrowser control, that works with the chromium engine. I choose this engine, because it allows to specify a proxy server just for the application.

如您所料,这个想法是创建一个内存中的代理服务器,并将您的 Web 浏览器控件重定向到该代理.

The idea, as you guess, is to create an in-memory proxy server, and redirect your web browser control to this proxy.

然后,FiddlerCore 发布一些事件,您可以在其中分析请求/响应,尤其是正文.

Then, FiddlerCore publishes some events where you can analyse the request/response, especially the body.

由于它充当代理,所有通信,包括 Ajax 调用、Flash 调用等,都由代理路由.

As it acts a proxy, all communications, including Ajax calls, Flash calls, etc, are routed by the proxy.

如果它可以帮助我帮助我对这种行为进行原型设计的小代码:

If it can help, a small code that help me to prototype this behavior:

应用程序.cs:

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        BootStrap();
        base.OnStartup(e);
    }

    private void BootStrap()
    {
        SetupInternalProxy();
        SetupBrowser();
    }

    private static void SetupBrowser()
    {
        // We may be a new window in the same process.
        if (!WebCore.IsRunning)
        {
            // Setup WebCore with plugins enabled.
            WebCoreConfig config = new WebCoreConfig
            {
                ProxyServer = "http://127.0.0.1:" + FiddlerApplication.oProxy.ListenPort.ToString(),
                EnablePlugins = true,
                SaveCacheAndCookies = true
            };
            WebCore.Initialize(config);
        }
        else
        {
            throw new InvalidOperationException("WebCore should be already running");
        }
    }

    private void SetupInternalProxy()
    {
        FiddlerApplication.AfterSessionComplete += FiddlerApplication_AfterSessionComplete;
        FiddlerApplication.Log.OnLogString += (o, s) => Debug.WriteLine(s);

        FiddlerCoreStartupFlags oFCSF = FiddlerCoreStartupFlags.Default;
        //this line is important as it will avoid changing the proxy for the whole system.
        oFCSF = (oFCSF & ~FiddlerCoreStartupFlags.RegisterAsSystemProxy);

        FiddlerApplication.Startup(
            0,
            oFCSF
            );
    }

    private void FiddlerApplication_AfterSessionComplete(Session oSession)
    {
        Debug.WriteLine(oSession.GetResponseBodyAsString());
    }
}

主窗口.xaml :

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1" 
        xmlns:Custom="http://schemas.awesomium.com/winfx" 
        x:Class="WpfApplication1.MainWindow"
        Title="MainWindow" Height="350" Width="525" Loaded="MainWindow_Loaded">
    <Grid>

        <Custom:WebControl Name="browser"/>

    </Grid>
</Window>

最后,MainWindow.xaml.cs :

And finally, MainWindow.xaml.cs :

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        browser.LoadURL("http://google.fr");
    }
}

您必须为此应用程序添加一些管道,以便路由和分析从应用程序到业务类的请求,但这超出了本问题的范围.

You will have to add some plumbing to this application, in order to route and analyse the requestion from the app to the business class, but that's beyond the scope of this question.

这篇关于WPF 网页浏览器的 LoadCompleted 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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