简体   繁体   中英

Send message from a WebBrowser control to the parent application

I have two applications:

  • A legacy one, written in .net
  • A modern web application with cool html5/js features

I want to make them coexist so i'm considering integrating a WebBrowser ActiveX control in my .net application. I want to understand how "deep" i can make them communicate and more specifically:

Which type of information can i send from the webapp to parent .net app?

In Microsoft documentation, several events are associated to the WebBrowser class like Navigated which seems to be triggered every time the webapp's url changes. So i can imagine a mechanism which:

  • change the url
  • without affecting the webapp content
  • but passes some info to the .net app.
  • example: navigating to http://my.web.app/#event=hello&content=you will tell me that event type hello was triggered with content you .

That sounds like a tedious process, so:

  • Is there something higher level that this idea? At the beginning, i expected something like the window.postMessage method?
  • If not, is there a library that provides a higher level api to perform such things more easily?

I think you can build a bridge between javascript code in modern application and your C# host application using this technique https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/implement-two-way-com-between-dhtml-and-client

It allows to execute C# code from JS as well as invoke JS from C#. You need to set ObjectForScripting property of WebBrowser control to C# object, methods of which will be called and then invoke it like

window.external.YourCSharpObjectMethod('called from JS code');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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