Many of my customers and students ask me:
What is XAML good for? Why do I have to write XAML if I can write WPF with code?
So I decided to write a post about it, sorted by the most important to the less essential.
1. It’s the Designer best friend
WPF is not just about another UI platform, it’s also a methodology. It provides new tools and concepts for creating a modern presentation layer. Programmers write code, but when it comes to drawing…, OMG!
To overcome this problem, a new member is introduced: a Designer. Designers usually are not developers, hence for, if, class, abstract, etc are all buzzwords for them. For a designer to feel at home, she has to work with familiar tools such as Adobe Illustrator, Expression Blend and Expression Design. Also she can read and write HTML and XML. Hence XAML is the ultimate markup for integrating the talents of both the Developer and Designer.
Express every visual aspect with XAML (or at least as much as you can) so the designer will be able to apply makeup on it.
2. It separates design from code
You may agree that it is a good design principle to separate the UI design from the code behind. This way it will be easy to change, share and replace the UI. Separating them further with two different languages provides a higher level of decoupling.
Developer write code with C#/VB.NET where Designer design UI with XAML.
3. It is toolable
Visual Studio is the best development environment ever. It provides so many tools for building projects, edit and compile files. But is it really necessary that Adobe Illustrator or Expression Design will do the same as it comes to WPF? Do they have to compile or generate C# or VB.NET code?
XAML is the best option for an application such as described above for exporting 2D and 3D Vector Graphics and Animation. It is more reasonable to export graphics as XAML, without dealing with C#/VB.NET, assemblies, code generation and other coding stuff.
XAML is XML so you can also write export plug-ins for other applications running on Linux or other operating systems.
4. It’s more securable than IL
XAML usually ends up with BAML, and code usually ends up with IL.
IL imposes a security risk where BAML is not!
5. It's hierarchical
Did you ever tried to initialize a hierarchical data such as Tree or data types based on the Composite Pattern within C#? I did that more than once, and each time I had a migraine.
Neither C# nor VB.NET is hierarchical language. Usually a programming language is flat, hence it is hard to deal with hierarchical data within code.
Because the XML nature of XAML, it is the perfect tool for creating and manipulating hierarchical data. WPF is hierarchical!
6. It can be easily parsed, serialized and desirialized
I love C#, but when it comes to parsing I better leave this painful job to the compiler team.
XAML is XML, it can be easily parsed serialized and desirialized. So in this case tools like XmlReader, XmlDocument, XamlReader and XamlWriter are my favorites.
7. It can be loose
Many talents UI developers use XML to describe dynamic menus, toolbars, commands, etc. The absurd is that the code for reading this XML at runtime and turning it into objects is written again and again.
Use loose XAML so you will never have to write this code again. It is perfect for this situation.
8. It’s more readable than code
When it comes to reading and understanding the structure of a UI, XAML is more readable than code.
Readable is a matter of taste, so I will leave it for Leo from The Matrix to decide.
9. It’s shorter than code
Uri uri = new Uri(@“Images\Photo.png”, UriKind.Relative);
BitmapSource bitmap = new BitmapSource(uri);
Image image = new Image();
Image.Source = bitmap;
Button button = new Button();
button.Content = image;
<Button>
<Image Source=“Images\Photo.png” />
</Button>
Who’s shorter?
10. It’s common
Try to Google “WPF”…
Hello…, is there anybody out there? C#? VB.NET? Helloooooo!?