简体   繁体   English

如何将数据从WebBrowser控件中的JavaScript返回到C#?

[英]How can I return data from JavaScript in a WebBrowser control to C#?

I have a WebBrowser control on my WinForm , within which I'm showing a dialog box with jQuery that contains a text area and a button labeled "save", the purpose of which is to obtain data from a user. 我的WinForm上有一个WebBrowser控件,其中显示了一个jQuery对话框,其中包含一个文本区域和一个标有“保存”的按钮,目的是从用户那里获取数据。

When the user clicks the save button I wish to retrieve the text in a text area and store it in a variable in my WinForm application. 当用户单击保存按钮时,我希望在文本区域中检索文本并将其存储在WinForm应用程序中的变量中。

Is this possible: jQuery function returning a value to a WinForm application? 这是否可能:jQuery函数将值返回到WinForm应用程序?

Yes it is. 是的。

If you plan to use the standard WebBrowser control for WindowsForm see this property: WebBrowser.ObjectForScripting -- There is a very useful example at the bottom showing you exactly how you would execute Javascript that calls back into your application. 如果您打算对WindowsForm使用标准的WebBrowser控件,请参见以下属性: WebBrowser.ObjectForScripting在底部有一个非常有用的示例,向您确切显示如何执行回调到应用程序中的Javascript。

Personally, I don't like the supplied WebBrowser control for WinForms or WPF -- They are simply COM Wrappers for internet explorer. 我个人不喜欢WinForms或WPF提供的WebBrowser控件-它们只是Internet Explorer的COM包装器。 I prefer to use Awesomium.NET . 我更喜欢使用Awesomium.NET

Do you specifically want to use jQuery to do this or do you just want to do bidirectional communication? 您是否特别想使用jQuery来做到这一点,还是只想进行双向通信? Using jQuery or traditional web practices will require a web server running somewhere for the page to query. 使用jQuery或传统的网络惯例将需要一台运行在某处的Web服务器以供页面查询。

If you just want bidirectional communication, set the WebBrowser.ObjectForScripting property. 如果只需要双向通信,请设置WebBrowser.ObjectForScripting属性。 This exposes a .Net object to the JavaScript code on the web page as window.external . 这会将.Net对象作为window.external公开给网页上的JavaScript代码。 The JavaScript code on the page can then call methods and properties on that object to communication with your WinForms application, like storing the text from the text area when the Save button is pressed. 然后,页面上的JavaScript代码可以调用该对象上的方法和属性,以与WinForms应用程序进行通信,例如在按下“保存”按钮时存储文本区域中的文本。

See http://msdn.microsoft.com/en-us/library/a0746166.aspx for more information. 有关更多信息,请参见http://msdn.microsoft.com/zh-cn/library/a0746166.aspx

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

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