Code behind in Flex and WPF

I should really have posted about Part 2b of my comparison of Flex and WPF, but I wish to jump ahead a little and compare the code-behind model in Flex and WPF. I am doing this because of a post I saw on Ted Patrick’s blog. Ted mentions about the code-behind model in Microsoft technologies like ASP.Net and shows how you can replicate that model in Flex. ASP.Net introduced the term Code-Behind with its first release and it has become a standard way of keeping the logic separate from the UI. It is a common theme across many MS technologies including WinForms and WPF. The code-behind model follows some file-name conventions which makes it easy for Visual Designers (think VS2005) to automatically keep the UI and the code-behind in sync.

In WPF…

Let’s take the case of the visual designer (either Expression Blend or Cider) for WPF. Since WPF uses XAML to describe the UI, the markup is kept inside <your-component>.xaml. The corresponding code-behind is separated out into <your-component>.xaml.cs. The code-behind is linked in the XAML file with the x:Class attribute on the root-tag. The code-generation part of the designer merges the xaml and the code-behind to create a single intermediate C# file. This file is named as <your-component>.designer.cs. This file is auto-generated and not meant for manual editing. The .designer.cs file is used for compiling to a .Net Type inside your application’s assembly. WPF is very strict about these naming conventions and also enforces the separation of UI and code-behind in separate files. In the earlier CTP releases of WPF it was possible to have inline C# code inside the XAML (using CDATA sections). However that feature has been removed from the RTM release.

In Flex…

Flex provides both the WPF style of code-behind (ie. Separate MXML and AS files) and the inline AS with the <mx:Script/> tags. This model is natural because Flex is using the standard HTML + CSS + Javascript paradigm of development. I think that makes it very flexible and it leaves to the developers to decide what is the appropriate model for them. I think most apps will use a mixture of these two approaches. Sometimes it makes sense to keep the code inline with the MXML (say for easier maintenance) and on some occasions when the code is really large the separate code-file approach works best. Ted’s post talks more of the later approach.

 

 

Technorati tags: , , ,
Advertisements

2 Responses to “Code behind in Flex and WPF”


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: