My Headlines

Monday, May 21, 2007

Enterprise WPF and Goodies

by Don Burnett

Well I have read a great entry this week on the business rationale for updating your "enterprise" software to take advantage of WPF technology. It's by Kurt Brockett . So if you are a business professional or a decision maker, you should check out his blog. He has a great discussion on when and why a business should invest and upgrade to WPF.

He not only really discusses how it will benefit but where and what type of an investment could really find a good return on investment. If your business is indeed thinking about the impact that RIA (Rich Internet Applications) will have on your ROI (Return on Investment) you should read his article.

And now for something completely different...

I have been working on turning my online portfolio website www.donburnett.com into a one-click rich internet application. I ran into a little bit of a snag. I wanted to include my blog in a tab on the WPF Application..

My first thought was to just include an RSS feed, but it turns out XAML doesn't directly address HTML. I have code to convert HTML to XAML but that all just seemed a bit to much work, plus the richtextbox control only handles RTF in XAML. 

Sure there is code out there to do this but it seemed a little (okay a lot) silly that there wasn't a web browser control in WPF 1.0. After all in the old winforms I can embed a browser anywhere..

The whole idea of an RSS feed though I could do it was seeming to not give me the formatting or the actual look of the original site.

My solution was to (for the first time) use the WindowsFormsIntegration.DLL that comes with the release of the .net 3.0 framework and just embed the webbrowser control from winforms right into that page.

Guess what folks? It worked great. After properly referencing the DLLs and all the appropriate framework controls (in both XAML and the Codebehind file).

It ended up just being four lines of code..

I placed the whole thing in a docpanel layout control and then referenceed the windowsforms host control then the web browser control, and I was DONE..

It looked like this:

<DockPanel Margin="0,28,0,25" x:Name="myFrame" Background="#000000">

<WindowsFormsHost x:Name="BrowserHost" >

<wf:WebBrowser x:Name="Browser" Url="
http://blog.donburnett.com" />

</WindowsFormsHost>

</DockPanel>

It was as simple as that and I get a cool WPF app that looks like this:

 

 

4 comments:

Unknown said...

Maybe using a Frame element and set the Source property "http://blog.donburnett.com" would have been faster and easier?

Unknown said...

Maybe using a Frame element and set the Source property to the Uri would have been faster and easier?

opherko said...

Don I've searched all over for proper integration of a COM object in a XAML based app and never got it to work. Probably a reference problem. I am glad to see you did it; I wish you had posted the code behind to see what references were included. Your blog did lead me to what I was looking for.

Benny, this is great. I don't know why I didn't find this use in the frame control before, but thanks you to both!!

Benny, you are right that was easier. 20 seconds and I'm displaying the HTML I was after in my WPF / XAML app. Much appreciated to both of you!

Don Burnett said...

Well, you have to remember you are talking about COM which is unmanaged CODE with WPF which is based on managed code. All of the WPF stuff runs inside of the .NET CLR which is basically a .NET application running in a virtual machine.

COM is unmanaged code and doesn't run inside the CLR, so the only mechanisms I know of is the integration support they added. Not sure that fits with what you need. Check out this.

http://www.codegod.de/WebAppCodeGod/integration-of-winforms-in-wpf-applications-AID405.aspx

http://sachabarber.net/?p=149

http://www.codeproject.com/KB/miscctrl/HostMFC.aspx