Windows Presentation Foundation: I must be missing something
I’ve spent most of the day playing around with the .NET Framework 3.0 RC1 bits. I’m primarily interested in the Windows Presentation Foundation (WPF) stuff, which deals with UI construction for both desktop and web apps under Vista/IE7 and beyond (though it works on XP and 2k3 Server too).
(As an aside, the new Windows x Foundation marketing idiom is among the more stupid in recent memory. That they’re bundled together into the .NET Framework 3.0, which has no obvious textual resemblance to the Windows somethingotherother Foundation template makes it all the more awkward. I liked WinFX better.)
Anyway, I grabbed the RC1 Windows Vista SDK, the .NET Framework 3.0 RC1 runtime, and the CTP “Orcas” preview stuff for Visual Studio 2005; this latter bit provides minimal (and I do_ mean _minimal) designer support and templates for WPF stuff.
I’ll start by saying the designer is for shit. The readme that ships with it basically says “it’s shit; it’ll probably get better before ‘Orcas’ ships”. The readme is a master of understatement. It took me about five minutes of XAML hacking to introduce markup that worked, did what I wanted, but broke the designer (the AllowTransparency property on a Window, in case you’re wondering). The designer is slow, redraws constantly, and craps out on the slightest error.
That said, the new split-screen interface, with the WYSIWYG designer on top and a XAML markup window underneath is a welcome innovation. Once it’s faster, sturdier, and more featureful, it’ll be a major upgrade.
Speaking of the XAML editor, it’s much more mature than the designer. The Intellisense surprised me with the number of places it was aware of context and could display the list of available properties. I’ve no complaints there.
Which brings me to the compiler. XAML is less a UI description language and more a general-purpose object persisence language; its primary use in WPF happens to be describing the object graph used to construct UIs. Thus, much like ASP.NET pages and their codebehind counterparts, XAML and its codebehind are compiled together to form the resulting binary. As such, the compiler catches errors in the XAML at build time, just like it would in CS files.
What sucks is that it seems to miss some errors that I would’ve thought it could detect at compile time (like missing resources, non-numeric values for numeric properties, etc). What sucks more is how this manifests itself: you run the app, and it eats shit and dies on an unhandled exception, either at startup or later depending upon where the error is. If you happen to trap this exception in the debugger, rather than seeing what the error is, you’ll see the Meaningless Exception of Doom, ‘the target threw an exception’. If you peel away about five layers of InnerException indirection, you’ll finally arrive at a meaningful error message, which in all fairness is usually pretty obvious.
This has got to change before release or Orcas, since it’s really easy to get XAML wrong as you’re learning and playing around with it. I’d like to see an error page-type display ala ASP.NET, with an excerpt of the code with the problem, the actual exception, etc.
And last, I find the Style and Template features of WPF to be woefully underpowered. When I first started reading the SDK docs about styles, I was heartened, as the feature seems well thought out and solid. In fact, it put me in mind of CSS, so much so that I assumed I could do CSS-like things with it. Mistake.
In CSS, you specify styles through a combination of a selector, which determines what elements you’re styling, and the style attributes themselves. The selector can be simple, like ‘all A elements’ or ‘the element having ID foo‘, but it can also be complex, like ‘all DIV elements inside a SPAN element which is inside a DIV element named foo‘. CSS needs this flexibility because people construct complex docuements and as such they require complex styles.
WPF, on the other hand, goes to great pains to make it easy to turn a button green when the mouse is over it, but fails miserably at providing a rich, flexible style function. Say I have a user control, and when it has input focus I want it to change its font to bold, and increase the opacity level on one of its child controls. Seems reasonable, right? You’d think, and you’d be wrong.
Despite being the subject of more sample code and breathless MSDN Magazine articles than any Microsoft buzzword technology before it, I can’t find any discussion or demonstration of the use of a style that defines properties for an element tree, or a way to gather multiple styles together and apply them en masse. To my mind, this is a serious limitation, and makes it feel like I’m back to coding HTML with CSS 1.0, which is rather ungrateful of me since it’s not like I had any of these abilities back in Winforms 2.0, but my sense of entitlement blinds me to the hardships of the past.
I’m hopeful that I’m just missing something, and that as I play with XAML/WPF a bit more I’ll get these issues ironed out, but experience suggests that’s alot to hope for…