XAML Guidelines

Jeffrey Smith / Wednesday, December 29, 2010

Whether you're a designer, a programmer or a hybrid, you need to work in a way that makes easier on members of your team. This comes from Martin Fowler's book Refactoring:

"There is another user of your source code. Someone will try to read your code in a few month's' time to make some changes. We easily forget that extra user of the code, yet that user is actually the most important. Who cares if the computer takes a few more cycles to compile something? It does matter if it takes a programmer a week to make a change that would have taken only an hour if she had understood your code."

XAML can get pretty dirty at times and often make it difficult for team members to find something in a file he or she did not write. There are no set guidelines for XAML in fact there is very little documentation on the subject, so really it's up to you to sit down with your team and figure out what will work best. I will simply layout some things that I think work well for my team.

  • The Name (x:Name) should always be the first Property of a Control.
  • Properties and Attributes should be on separate lines.
  • Properties like Alignment can be on the same line.

Some would make the argument that this would make longer XAML files and waste space but to Martin Fowler's point it's not about you or the computer, it's about other people being able to read and quickly assimilate to your code.

A few other things to consider:

  • Avoid Abbreviations, Like Btn for Button Pnl for Panel, just spell it out
  • Include the type of control in the name and the name of the style
  • Start Storyboard Names with sb