简体   繁体   English

需要一种方法来捕获WinForm Webbrowser控件中的javascript事件

[英]Need a method to capture javascript events in WinForm webbrowser control

I have a WinForms app that uses a .NET webbrowser control. 我有一个使用.NET Webbrowser控件的WinForms应用程序。 What I need to do, is wire up an event on the WinForms side to fire when a value is set (via javascript) in the loaded HTML page. 我需要做的是,在加载的HTML页面中设置一个值(通过javascript)时,在WinForms一侧连接一个事件以触发该事件。 I had success doing this with an onclick event of a button, but I can't seem to get it to work with a custom event. 我通过按钮的onclick事件成功完成了此操作,但似乎无法使其与自定义事件一起使用。 I don't know if this is a limitation in what the browser control can attach to event wise. 我不知道这是否限制了浏览器控件可以附加到事件方面。

So essentially, I need that when a JS function is called in the HTML page and sets a value of a hidden input element (or it could be a regular input that I style to be hidden), I need to know that in WinForms. 因此,从本质上讲,我需要在HTML页面中调用JS函数并设置隐藏输入元素的值(或者它可能是我要隐藏的常规输入)时,我需要在WinForms中知道这一点。 If it helps, I am using browser flags in this application to emulate IE11 instead of the default IE9 engine. 如果有帮助,我将在此应用程序中使用浏览器标志来模拟IE11,而不是默认的IE9引擎。 The HTML page loaded is also mine so I can modify it any way needed to make this work properly. 加载的HTML页面也是我的,因此我可以对其进行任何修改以使其正常工作。 I would just use the onclick events of the buttons, but this is a gmaps integration where there can be upwards of 2000 buttons generated (one per marker placed) so it seems like a huge waste of resources to wire up 2000 onclick events when any of those button clicks only modify 4 input fields with the data I care about. 我只使用按钮的onclick事件,但这是一个gmaps集成,其中最多可以生成2000个按钮(每个标记放置一个),因此在以下任何情况下连接2000个onclick事件似乎很浪费资源这些按钮单击只会使用我关心的数据修改4个输入字段。

This project happens to be in VB.NET, but C# solutions would be fine as well. 这个项目恰好在VB.NET中,但是C#解决方案也可以。 They can be transcoded or if the solution uses C# specific features, we can move this to a separate DLL and reference it. 它们可以被转码,或者如果解决方案使用C#特定的功能,我们可以将其移至单独的DLL并进行引用。

After spending a lot of time on this today, I found a solution. 今天花了很多时间后,我找到了解决方案。 It isn't a direct solution to the problem I posted, but it got me to where I needed to be. 这不是我发布的问题的直接解决方案,但是它使我到达了需要的位置。 I am still interested in an answer to the original problem if anyone has one, but for now, what I found I could do was to create a class in .NET that I could assign to the ScriptingObject of the browser control and then call window.external.myFunctionName, where myFunctionName is a function within the .NET class. 我仍然对如果有人有一个原始问题的答案感兴趣,但是现在,我发现我能做的是在.NET中创建一个类,该类可以分配给浏览器控件的ScriptingObject,然后调用window。 external.myFunctionName,其中myFunctionName是.NET类中的函数。 This works great for my specific problem, but would not work if I didn't also control the HTML page I was consuming with the browser. 这对于我的特定问题非常有用,但是如果我也无法控制浏览器正在使用的HTML页面,那将无法正常工作。 That is why I am still interested in alternate solutions if anyone has one. 这就是为什么我仍然对替代解决方案(如果有人有)感兴趣的原因。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM