asp.net – 如何以WPF格式捕获网页的按钮点击事件(在WebBrowser
发布时间:2020-12-16 07:03:50 所属栏目:asp.Net 来源:网络整理
导读:考虑一下我在 WPF应用程序中有一个WebBrowser控件的场景. Web页面在WebBrowser Control中加载.该网页包含一个按钮. 该网页是ASP.NET应用程序. 我想将网页的按钮点击事件捕获到WPF表单(主持WebBrowser控件).有没有办法实现这个功能? 谢谢, 塔潘 解决方法 这
考虑一下我在
WPF应用程序中有一个WebBrowser控件的场景.
Web页面在WebBrowser Control中加载.该网页包含一个按钮. 该网页是ASP.NET应用程序. 我想将网页的按钮点击事件捕获到WPF表单(主持WebBrowser控件).有没有办法实现这个功能? 谢谢, 塔潘 解决方法
这里的代码应该完全按照你想要的方式用注释来解释发生了什么:
public partial class MainWindow : Window { /// <summary> /// This is a helper class. It appears that we can't mark the Window as ComVisible /// so instead,we'll use this seperate class to be the C# code that gets called. /// </summary> [ComVisible(true)] public class ComVisibleObjectForScripting { public void ButtonClicked() { //Do whatever you need to do. For now,we'll just show a message box MessageBox.Show("Button was clicked in web page"); } } public MainWindow() { InitializeComponent(); //Pass an instance of our helper class as the target object for scripting webBrowser1.ObjectForScripting = new ComVisibleObjectForScripting(); } private void Window_Loaded(object sender,RoutedEventArgs e) { //Navigate to your page somehow webBrowser1.Navigate("http://www.somewhere.com/"); } private void webBrowser1_LoadCompleted(object sender,NavigationEventArgs e) { //Once the document is loaded,we need to inject some custom JavaScript. //Here is the JavaScript var javascript = @" //This is the JavaScript method that will forward the click to the WPF app function htmlButtonClicked() { //Do any other procession...here we just always call to the WPF app window.external.ButtonClicked(); } //Find the button that you want to watch for clicks var searchButton = document.getElementById('theButton'); //Attach an onclick handler that executes our function searchButton.attachEvent('onclick',htmlButtonClicked); "; //Grab the current document and cast it to a type we can use //NOTE: This interface is defined in the MSHTML COM Component // You need to add a Reference to it in the Add References window var doc = (IHTMLDocument2)webBrowser1.Document; //Once we have the document,execute our JavaScript in it doc.parentWindow.execScript(javascript); } } 其中一些来自http://beensoft.blogspot.com/2010/03/two-way-interaction-with-javascript-in.html (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 将RSS pubDate格式化为.NET DateTime
- 从ASP.NET中的标准程序集覆盖资源
- asp.net – 如果禁用ViewState会发生什么
- asp.net – 动态下载链接算法
- asp.net-mvc-4 – 带有返回URL的注销链接(OAuth)
- 自定义每个用户的会话超时 – ASP.NET
- asp.net-mvc-4 – 在哪里可以找到WebMatrix.WebData.WebSec
- asp.net – 有浏览器相当于IE的ClearAuthenticationCache?
- 如何在Asp.Net中使用CSS创建多列数据输入表单?
- asp.net – c#如何获取httpResponse.BinaryWrite处理的流
推荐文章
站长推荐
- asp.net-mvc – Asp.net MVC5,如何加载.hbs文件
- asp.net – 什么是建议替代常见的破坏app_offlin
- “自动完成关闭”无法在IE中运行 – ASP.Net
- asp.net-mvc – 仅将值从EditorTemplate传递到其
- ASP.NET缩小并连接App_Themes CSS文件
- asp.net-mvc-3 – asp.net mvc 3和elmah.axd –
- asp.net-mvc – 使用Ajax.BeginForm绑定HttpPost
- asp.net – 我从添加引用对话框中找不到System.W
- asp.net-mvc – 使用实体框架的没有存储库模式的
- 使用Bitbucket Pipeline进行.Net Core项目的自动
热点阅读