The Ultimate Intranet Toy: Windows.Forms.WebBrowser and window.external
Without pain and struggle, I desire to have a web browser embedded in my desktop app, and have my C# event handlers seamlessly play nicely with web pages' JavaScript events — without enough COM interop attributes to kill a moose at 300 feet. I have seen the magic that is WebBrowser and dub it impressive. As the title implies, this is not a trick for the public Internet, it relies on FullTrust and Internet Explorer. But for those of us making whiz-bang software for big corporations, this is pretty cool.
I created a sample application to play around with the control (VS2005 & ASP.NET AJAX required.) People have been putting ActiveX browser controls in their apps for years, but getting them to talk nice with their containers has always been awkward. Possible, yes, but awkward. The good people at Microsoft wrapped the old web browser control and made it .NET friendly. The sample app runs a form with a web browser in it. The web browser can do its own postback stuff, and it can also, via JavaScript, talk to the containing form. The beauty of it is how little code is required.
The app starts up with a web page whose "Server Postback" button merely increments the value found in the postback.
Press Server Postback — it's really just a web page.

Now you want the client-side button to talk directly to your form without lots of madness. Easy. Register your form as ComVisible, then tell the WebBrowser object you'd like it to expose your Form to the JavaScript running in the browser. Like so.
Now write the function which the JavaScript will call. COM reflection happens magically, so all you need to do is expose a public method.
And call it in JavaScript. How does JavaScript know where to magically find your scriptable object (i.e. your form in this example?) The window.external property magically points at your object, that's how.
For anyone who's struggled with all the nasty Interop tricks needed to play nice with the old ActiveX control or all the bizarre HTML tricks needed to play nice with Smart Clients embedded in HTML, this is a godsend. I look forward to abusing it! The documentation gives fair warning that this object must be hosted and managed from the main STA UI thread of your Windows.Forms app, but what else is new. Now, off to embed this in an AGX task.





1 Comments:
Just 2 years too late :-(
Post a Comment
Links to this post:
Create a Link
<< Home