This can be considered as a continuation of one old post in this same blog. In that post we can see the technique to establish communication between HTML Page inside WebBrowser controls and and .Net application. In this post we are going to see the approach in detail. Code is available for download and is self explanatory.
- For WPF to JS communication
- WebBrowser control has a method called InvokeScript.
- For Web page/JS to WPF .Net communication
- WebBrowser has a property called ObjectForScripting which will be available in JS as window.external
- If we set an object to ObjectForScripting property, that .Net object will be available in JS and from JS we can call any method on that object which will in turn call the .Net code.
What’s not done
This just proves the technique. Performance testing is not part of this.
- The HTML page is inside the WPF.
- WebBrowser loads the HTML page by using pack: uri syntax.
- WebBrowser's ObjectForScripting Object is set when the window loads
- A class which is comvisible is used for the above property
- For WPF to JS myWebOC.InvokeScript() is invoked
- In JS the data is received in the setSomeText() function.
Download from the below url.