Tuesday, April 08, 2008

Thought on WPF

I recall that some time I go I was having a conversation with my colleague about WPF. His complaint was that the XAML thing is really complicated, messy and difficult to comprehend. While I am supporting the blue badge camp, there is in fact some truth to his statements and this lead to an interesting discussion and thought.

WPF and XAML is kind of a revolutionary, and it requires a new way of thinking and accepting it. Although XAML should feel natural to developer and designer who are familiar to HTML, the expressiveness of XAML is extended way beyond of what we can used to imagine. Just like HTML, XAML is very good and simple in specifying the UI layout and content without needing to think through the object model. However, as the UI get more complex and the XAML growth, it become more difficult to manage a large chunk of XAML.

Second, the tool support for WPF is still pretty weak. Visual Studio 2008 and Expression Blend each has a WPF UI designer, but neither are really strong. Visual Studio 2008 are sufficient for creating simple WPF application but is not the right tool for creating complex, fancy and interaction rich application. Expression Blend fill in this gap but the shortcoming is it does not have intelisense support and the XAML editor does not support the same short cut key as in Visual Studio. Add on to this is the performance of the WPF designer in both tool sucks! The debug-ability of XAML application is also very weak. The best thing Visual Studio does now is just to throw an exception in its InitializeComponent() method whenever there is an error in the XAML. It is up to the developer to figure out what went wrong. To be fair, WPF is still a relatively new thing, and we should give the blue badge guy more time to get the tool right. Hopefully it won’t take long for this to happen.

WPF introduce a lot of new concepts such as property element, markup extension and so on. Developer who are not familiar with XML has even a harder time to understand how XAML is structured (I had a colleague who just managed to figure out what XML namespace is recently). Using markup extension in XAML really takes some time to figure out how it works because of the way it is expressed in attribute value string and the special syntax notation it had.

Enough rambling about WPF and XAML. XAML does have some good thing that we should praise about. Most notably, The expressiveness of XAML makes certain expression easier to understand than in imperative code. For example, let say I have a button on the form which width I want it to automatically adjust based on a value in a textbox that the user can type in. The most obvious way and simplest to do this is using data binding. Specifying in XAML will look like this :

<WrapPanel>
<Button x:Name="btnOk" Content="Clickme"
Width="{Binding Path=Text, ElementName=txtWidth, Mode=Default}" />
<TextBox x:Name="txtWidth" Text="120"/>
</WrapPanel>


To achieve the same effect in C#, the code will look like this:

Binding myBinding = null;
myBinding = new Binding("Text");
myBinding.ElementName = "txtWidth";
btnOk.SetBinding(Button.WidthProperty, myBinding);


or
Binding myBinding = null;
myBinding = new Binding("Text");
myBinding.ElementName = "txtWidth";
BindingOperations.SetBinding(btnOk, Button.WidthProperty, myBinding);


Neither of the C# code is any easier to understand than the XAML code. On top of that, it is not easy and obvious to figure out the right API and object model to do it in C# code. When I type btnOk.width in Visual Studio, intellisense show that width property is of type double, thus it seem that there can be nothing much that can be done about it. So, XAML win in this aspect.

The XML nature of XAML allow us to visualize better the layout hierrarchy of UI controls. The ability to define resources such as style and templates in XAML right at the appropriate context and being able to specify complex UI controls in the Content property in XAML add on to the clarity and simplicity of the UI definition than doing it at code level.

WPF and XAML is still weak is some areas especially the tooling support. But nonetheless, this should not hinder us from embracing a new technology. The technology landscape is constantly changing and we should always approach new technology with a new and open mindset.

Labels:

0 Comments:

Post a Comment

<< Home