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

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

问题描述

在将WPF的web浏览器的LoadCompleted事件触发? 是本次活动等待任何Ajax调用来完成的aspx页面。

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

我有一个WPF应用程序在web浏览器控件放置在窗的形式,并使用导航方法加载一个网页上。该LoadCompleted事件触发,即使一些Ajax调用未初始化/等待。

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.

您将需要一些第三方组件:

You will need some 3rd party assemblies :

  1. FiddlerCore :将作为一个HTTP代理,内嵌在应用程序中
  2. Awesomium.net :WebBrowser控件,与铬发动机的工作原理。我选择这个引擎,因为它允许指定代理服务器只为应用程序。
  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.

如果它可以帮助,小code,帮助我为原型这种行为:

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

App.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());
    }
}

MainWindow.xaml:

MainWindow.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 web浏览器的LoadCompleted事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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