WPF Web浏览器控件与Winforms [英] WPF webbrowser control vs winforms

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

问题描述

我正在使用Webbrowser控件创建一个wpf应用程序.无论如何,有时我需要查找html元素,调用点击和其他基本功能.

I am creating a wpf application where I am using a webbrowser control. anyways sometimes I am required to look for html elements, invoke clicks, and other basic functionality.

在winforms Webbrowser控件中,我可以通过执行以下操作来实现此目的:

In winforms webbrowser control I am able to achieve this by doing:

 webBrowser1.Document.GetElementById("someId").SetAttribute("value", "I change the value");

在wpf webbrowser控件中,我通过执行以下操作来实现相同的目的:

In wpf webbrowser control I managed to achieve the same thing by doing:

  dynamic d = webBrowser1.Document;  
  var el = d.GetElementById("someId").SetAttribute("value", "I change the value");

我还设法通过使用动态类型在wpf Webbrowser控件中调用了一个单击.有时候我会出神.

I also managed to invoke a click in the wpf webbrowser control by using the dynamic type. Sometimes I get exeptions though.

我如何能够在wpf Webbrowser控件中查找 html元素,设置属性和调用点击,而不必使用经常发生异常的动态类型?我想用wpf webbrowser控件替换wpf应用程序中的winforms webbrowser控件.

How will I be able to look for html elements, set attributes and invoke clicks in a wpf webbrowser control without having to use dynamic types where I often get exceptions? I will like to replace my winforms webbrowser control in my wpf application by a wpf webbrowser control.

推荐答案

我这样做的方法是...

The way I have done this is...

使用HTTPRequest下载要呈现的页面的HTML文本.在HTML文本中使用HTML敏捷包注入Java脚本.如果要使用jQuery,则必须先jQuerify页面,然后将事件与dom元素绑定.您也可以从脚本内或其他方式调用c#函数. 不会弄乱动态类型,因此也不例外.

Download HTML text of the page you want to render using HTTPRequest. Inject java script using HTML agility pack in HTML text. If you want to use jQuery then you have to jQuerify your page first and then bind event with your dom elements. You can also call your c# function from within the script and other way around. No messing around with dynamic types and hence no exception.

您还可以在此 查看全文

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