WPF WebBrowser:我如何访问进度和新窗口事件
我正在构建一个使用 WebBrowser 控件的 WPF 应用程序。
我在以下几点上苦苦挣扎:
如何从控件获取下载的当前进度。 WinForms WebBrowser 控件引发 ProgressChange 事件 - 如何使用 WPF 变体复制此功能?
如何捕获尝试在新窗口中打开的链接。 Winforms WebBrowser 再次有一个 NewWindow 事件。我可以使用它来停止 IE 的启动并在同一窗口中打开链接。是否可以使用 WPF 变体来执行此操作?
I'm building an a WPF app that uses the WebBrowser control.
I'm struggling on a couple of points:
How to get the current progress of a download from the control. The WinForms WebBrowser control raises ProgressChange events - how can I replicate this feature with the WPF variant?
How to capture links that are trying to open in a new window. Again the Winforms WebBrowser had a NewWindow event. I could use this to stop IE being started and to open the link in the same window. Is it possible to do this with the WPF variant?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要获取
IWebBrowser2
接口,有一个简单的方法:WebBrowser 类有一个属性
AxIWebBrowser2
,它保存底层 COM 浏览器对象,但它是“内部”的,因此我们可以通过反射得到它。To get
IWebBrowser2
interface, there is an easy way:The WebBrowser class has a property
AxIWebBrowser2
, and it holds the underlying COM browser object, but it is "internal", so we can get it by reflection.找到我想要的信息后,我想我会为感兴趣的人更新这个问题。
在 http 的底部://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser(v=VS.90).aspx 有一条评论,标题为“获取本机 IWebBrowser2”。
这显示了如何到达所需的界面并且似乎运行良好。
编辑:在 MSDN 上的评论不断消失的情况下,在此处添加链接内容。
我们的托管包装器尚未公开本机 Web 浏览器控件的许多功能。以下代码片段显示如何从 WPF WebBrowser 控件获取
IWebBrowser2
接口。这允许访问对象上未以其他方式公开暴露给控件的方法。但请注意,此代码示例仅适用于完全受信任的代码。首先,请参阅此处的 IWebBrowser2 文档: http://msdn.microsoft.com/en -us/library/aa752127.aspx ...
要编译此代码,请将 COM 引用添加到
System32\shdocvw.dll
或ieframe.dll
(以哪个为准)您有,具体取决于 IE 版本)。然后 myWebBrowser2 就可以进行交互了。
您还可以处理本机 Web 浏览器的事件 (http:// /msdn.microsoft.com/en-us/library/aa768309(VS.85).aspx) 通过生成的托管包装器,如下所示:
Having found the information I wanted I thought I'd update this question for anyone interested.
At the bottom of http://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser(v=VS.90).aspx there is a comment entitled "Getting to the native IWebBrowser2".
This shows how to get to the required interface and seems to works well.
EDIT: Adding content of the link here as the comments at MSDN keep disappearing on me..
There is much functionality of the native Web Browser control that our managed wrapper does not yet expose. The following code snippet shows how to get the
IWebBrowser2
interface from the WPF WebBrowser control. This allows access to methods on the object that are not publicly exposed in other ways for the control. Do note, however, that this code sample will only work in fully trusted code.First, see IWebBrowser2 documentation here: http://msdn.microsoft.com/en-us/library/aa752127.aspx ...
To compile this code, add a COM Reference to
System32\shdocvw.dll
orieframe.dll
(whichever you have, depending on version of IE).And then myWebBrowser2 is ready for interaction.
You can also handle the native web browser's events (http://msdn.microsoft.com/en-us/library/aa768309(VS.85).aspx) through the generated managed wrappers, like this: