Firefox, WPF and XBAP
I finally got around to trying a .NET 3.5 XBAP (XAML Browser App or "WPF Browser Apps") in Firefox, and it works just as advertised. I put together an incredibly useful and powerful application consisting of a picture of me and a button. I think everyone will want to run it. ;)
Seriously though, it's very easy to deploy apps like this. This reminds me of the year I spent working for Aurum (then a division of Baan) creating a large application using VBPs - ActiveX Documents.
These were the same basic idea. Your application would run - then IE4, methinks - using the Browser as it's Window Frame, much the way Word or Acrobat can open up a document inside the Browser. This is all still very old-school ISite COM stuff.
Anyway, XBAPs aren't Silverlight, they are the Full .NET Framework in your browser. With .NET 3.5 that means IE or Firefox. Think of XBAPs as ClickOnce applications that never jump out of the browser.
Keep in mind that mine is a silly example, and yes, this one could be done with DHTML, however the Woodgrove Finance Application (a .NET 3.0 WPF Application, seen above in Firefox) would be more challenging, hence the idea behind WPF Browser Apps.
I fire up VS2008 and hit File | New Project | WPF Browser Application.
Then, I drag an image on the Page and a Button. I use Split Screen View so I can see the XAML being written a the same time. I double-click on the surface, then go back and double-click on the button. That adds the Loaded= and Click= event handlers you see below. I could have typed this manually also.
<Page x:Class="WpfBrowserApplication1.Page1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Page1" Loaded="Page_Loaded"> <Grid> <Button Height="57" Margin="70,0,70,24" Name="button1"
VerticalAlignment="Bottom" Click="button1_Click">What a great App!</Button> <Image Name="image1" Margin="50,18,48,96" > <Image.RenderTransform> <RotateTransform Angle="0" CenterX="100" CenterY="100" /> </Image.RenderTransform> </Image> </Grid> </Page>
I'm going to do two things. One, I'll load a picture from an Embedded Resource. I could have loaded it from a location like my web server, but this is harder and educational for me. Then, two, I'll make the picture turn when I push the button. I could add an Animation declaratively but again, this is a little harder and more interesting.
First, getting the embedded graphical resource. This might look familiar if you've done it with WinForms.
System.IO.Stream stream = this.GetType().Assembly.
GetManifestResourceStream("WpfBrowserApplication1.MyFile.jpg"); JpegBitmapDecoder bitmapDecoder =
new JpegBitmapDecoder(stream,
BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.Default); ImageSource imageSource = bitmapDecoder.Frames[0]; image1.Source = imageSource;
I've made it more lines than is needed, but basically, get the stream, decode the graphic (Gif, Jpeg, PNG) and grab the Frame. If it were a Gif, it might be animated, hence Frames[0].
Ok, easy. Now to make it spin. And not just turn in a chunky way, but smoothly turn 60 degrees with each button press. I make an animation, set a From and To value as a double and tell it to last a half second. Then I grab the RotateTransform off the image and begin the animation.
private int angle = 0; private void button1_Click(object sender, RoutedEventArgs e) { DoubleAnimation ani = new DoubleAnimation(); ani.From = angle; angle += 60; ani.To = angle; if (angle >= 360) angle = 0; ani.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 500)); RotateTransform tran = (RotateTransform)image1.RenderTransform; tran.BeginAnimation(RotateTransform.AngleProperty, ani); }
There's a pile of good articles and books out there on WPF. What strikes me is how many high level it is. It's nice to think of constructs like angle and RotateTransform rather than doing the math.
The silly result of this code is at http://www.hanselman.com/clickonce/takethree/WpfBrowserApplication1.xbap and it works in both Firefox and IE if you have .NET 3.5 installed.
Related Posts
- WPF Contest and Prizes! Ends Feb 29th, 2008!
- 3D Chess in the Browser by Valentin Iliescu
- 3D Physics XBAP by Chris Cavanagh and Source
- It's been one of those Old School Weeks - ActiveX Documents
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
For both the applications above, I'm prompted by Firefox to open using the 'WPF Host' in a new tab. Selecting Ok begins triggers another tab to open, same prompt, same result and son on until either I stop, hit cancel and close all the empty tabs or I let Firefox die under the burden of 50 tabs....
Am I alone with this problem ? It's an issue I've always had with Firefox, Vista and XBaps....
Cheeer
ian
Just an out of topic comment....
With all the work you do, all the articles you write, I often wonder how you get to manage your time. I think an article on your blog about how practically(efficiently!) you manage your time would be very useful to noobs like me and others who follow your blog. I hope you make time for that and write one very soon.
Wish you a great time and good luck.
Krishna Vangapandu
What is that advantage of constraining the application to within the browser's window (and yet not following any of the conventional browser rules)?
I always like to know what happens behind the scenes, i downloaded the xbap and the exe.manifest and it worked locally, I'm assuming the first time i requested the page it downloaded an exe and hid it somewhere? where is that?
I wanted to suggest a XamlViewFactory for MVC, but I'm not sure how that would work if it's just a compiled exe. It would be great to have rich content like this embedded in a page and be able to pass data to it. any thoughts?
I guess you'll say this isn't traditional web development anymore, it's Client development but deployed in a browser?
(For the records the 15% (more or less) code we cannot share with the desktop client include security-induced workarounds (because XBAPs in Internet cafés run in partial trust) and of course the webclient-to-webserver communication. In that case however, we can use WCF and enjoy a much nicer communication that using standard SOAP and JavaScript-based web services.)
HTH,
Laurent
his has potential when paired (or even to replace) with my click-once apps.
We did work in .NET 3.5 to unify the browser and XBAP cookie stores, to allow XBAPs to access session and persistent cookies from the browser cookie store and vice versa. Cookies set by the browser for domains that XBAP web requests make will be attached automatically to those requests. So, you can pass session state in XBAPs via cookies.
Please see http://msdn2.microsoft.com/en-us/library/ms750478.aspx#Cookies
What’s new in WPF in .NET 3.5 http://msdn2.microsoft.com/en-us/library/bb613588.aspx
(2) It is not a total panacea, though. Setting: Newly installed Windows XP sp2, with .NET Framework 3.5 and updated Firefox 2. When I open the example application, all I got is a "Mac-like" error message that "An error occurred". Attempting to restart of course does not help as this occurs on startup. When I click on the More Information button, nothing happens. My concerns are that you are just trading one set of deployment problems for another.
RotateTransform tran = (RotateTransform)image1.RenderTransform;
ERROR:
Unable to cast object of type 'System.Windows.Media.MatrixTransform' to type 'System.Windows.Media.RotateTransform'.
Do you have any ideas on why I would get this error. I just copied your code into a test app and used a jpg of mine.
Great app, BTW! ;-)
Thanks for your help!
2) Is it possible to place ASP.NET Server control onto XBAP page?
Thank you.
this web site is very good for us....but we need more Console Application.....in c# 2005 video than we can inprove more ......
so, plz ...
keep the video of console application ,
thankyou......
Rojariya..
This is off topic, sort of. Do you feel ASP.NET and the .NET 3.5 Framework are ready for production applications? In this case, the redesign of our Intranet. WPF looks quite attractive, especially for our dashboard.
Thanks in advance,
Robert Hankin
That cookie thing was fixed in .NET 3.5: Hey, got this message from the PM for XBAPs and I'll post samples soon.
That cookie thing was fixed in .NET 3.5: Hey, got this message from the PM for XBAPs and I'll post samples soon.
That's nice but there's no OO abstraction to access cookies. Just two methods.
Locked up Firefox 2.0.0.12 with the No Script add on. A reload (restore session) did a download and it worked.
I am looking into this for a lob app i am writing. Kind of new to wpf altogether...any issues with writing something like this that consumes client application services (for auth?).
thanks - Rob H.
http://chriscavanagh.wordpress.com/2008/02/29/wpf-animated-gif-and-javascript-filmroll/
It's a short example using my "SkinBuilder" utility to generate animated gifs and/or javascript-driven filmrolls from WPF 3D animations, with anti-aliasing! :)
It's also a way of getting some WPF love to the needy people who are without WPF or Silverlight 2.0 :) (or an excuse to play with WPF for website design).
Chris
WPF would be great for content management, but most web-developers need to support all of the major browsers on Windows, Mac and Linux at least.
Comments are closed.