使用 WebBrowser 控件的 WPF 设计注意事项 [英] WPF Design Considerations Using WebBrowser Control

查看:24
本文介绍了使用 WebBrowser 控件的 WPF 设计注意事项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须构建一个程序,让我能够从系统中跟踪被击中的不同触发点.

I have to build a program that will allow me to track from a system different trigger points being hit.

当另一个系统中的触发器被调用时,Web 服务将被调用,该 Web 服务将记录到数据库中:

When a trigger has been hit within another system a web service is called, this web service will log to a database the following:

  1. 会员 ID
  2. 触发器 ID
  3. 参考代码

我的程序是读取这些数据并调用对附属网站的调用以进行附属跟踪.

My program is to read this data and invoke a call to affiliates websites for affiliate tracking.

唯一的问题是,这些网站不提供任何好的网络服务或表单发布,它是通过跟踪像素(从其服务器上的某种进程创建的图像)或 javascript 来完成的.

The only trouble is, these sites do not offer any nice web service or form post, it is done via tracking pixels (images created from some kind of process on their server) or javascript.

所以我一直在尝试让它工作,我能想到的唯一方法是通过使用WebBrowser"控件的 WPF 或 WinForms 应用程序.

So I have been playing around with this trying to get it working, and the only way I can think of doing it is via a WPF or WinForms application using the "WebBrowser" control.

因此,我只需从数据库中获取结果集,并为捕获的每个触发器获取所需的会员跟踪图像代码或 JavaScript 代码,然后使用参考号、会员 ID 等替换预定义的键.

So I simply get the results set out of the database and for each trigger captured get the required affiliate tracking image code or javascript code and then do replacements on predefined keys with the reference numbers, affiliate ids, etc.

我在这里担心的是,由于 WebBrowser 控件是异步的,因此在我要求 Web 浏览器加载另一个跟踪代码片段之前,它可能不会处理 javascript 或图像请求.

My concern here, is as the WebBrowser control is asynchronous it may not process the javascript or image request before I ask the web browser to load another tracking code snippet.

List<TRACKING_TRANSACTIONS> trackingTransactions = affiliateContext.TRACKING_TRANSACTIONS.Where(at => at.EFFECTIVE_DATE <= DateTime.Now && at.PROCESSED_DATE == null ).ToList();

foreach (TRACKING_TRANSACTIONS transaction in trackingTransactions)
{

    if (transaction != null)
    {
        AFFILIATE_TRIGGERS affiliateTrigger = affiliateContext.AFFILIATE_TRIGGERS.SingleOrDefault(at => at.AFFILIATE_ID == transaction.AFFILIATE_ID && at.TRIGGER_ID == transaction.TRIGGER_ID);

        if (affiliateTrigger != null)
        {
            //do replacements
            string trackingCode = ReplaceStringValues(affiliateTrigger.TRACKING_CODE, transaction);

            RunWebBrowserTrackingCode(trackingCode);//ConfigurationManager.AppSettings["WebsiteUrl"] + "?trackingTransactionId=" + transaction.TRACKING_TRANSACTION_ID.ToString());        
            Thread.Sleep(2000);

            transaction.PROCESSED_DATE = DateTime.Now;

            affiliateContext.SaveChanges();
        }
    }


}

对网络浏览器的调用在这里

The calls to the web browser are here

void RunWebBrowserTrackingCode(string trackingCode)
{

    if (!webBrowser.Dispatcher.CheckAccess())
    {
        webBrowser.Dispatcher.Invoke(
        System.Windows.Threading.DispatcherPriority.Normal,
            new Action(
            delegate()
            {
                webBrowser.NavigateToString(trackingCode);
            }
        ));
    }
    else
    {
        webBrowser.NavigateToString(trackingCode);
    }
}

我主要担心的是,使用 Thread.Sleep(2000); 并使程序等待是错误的做法.

My main concern is that using Thread.Sleep(2000); and making the program just wait is the wrong way of doing it.

谁能提出更好的方法?我是否使用正确的技术解决了这个问题?

Can anyone suggest a better way? Have I even tackled this using the right technologies here??

推荐答案

看一下 DocumentCompleted 事件:

Take a look at the DocumentCompleted Event:

http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.ondocumentcompleted.aspx

这篇关于使用 WebBrowser 控件的 WPF 设计注意事项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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