-1

I miss something similar to the CleanCode book for xaml. How should I structure the XAML?

? All attributes in one line? One attribute per line?

<TextBlock Grid.Column="2"
           Margin="2"
           Text="{Binding TotalCheckedTagCount, FallbackValue=0}" 
           Foreground="Black"
           FontWeight="Bold"
           FontSize="14" 
           HorizontalAlignment="Right"
           />

? Which properties first? The Gridposition? The name? The Binding?

? Should I put the closing xml tag on the same line or on a new one?

<TextBlock
           <!-- .... -->
           />

<TextBlock
           <!-- .... -->  />

and so on... I'd just like to know if there is already an existing guide before I spent time with creating one myself. So far I've had no luck in finding one.


Edit: Two interesting guides provided by mm8:

https://github.com/cmaneu/xaml-coding-guidelines

https://paulstovell.wordpress.com/2007/05/04/xaml-and-wpf-coding-guidelines/

They cover topics like, naming, attribut placment, closing tags, resources handling etc.

Hans Vader
  • 183
  • 10
  • If you find your self adding a lot of tags to your elements consider putting them in your `Styles`, use `DataTemplates` `Dictionaries` to minimise the number of elements that you have to add. – XAMlMAX Jun 30 '17 at 07:46
  • yeah I do that, but the styles and templates itself are pretty huge too. I just feel like there has to be a guide which makes such big XAMLs more readable. I mean otherwise you just have a huge inherit-structure like google does with their material style. – Hans Vader Jun 30 '17 at 07:48

2 Answers2

3

I can just write the experiences I made during the years.

  • I would put each tag in a new line because it is easier to read, get an overview and easier to merge
  • I would out the closing tag behind the last tag (just like it that way)
  • I would put the most important tags at the start. The most important tags for me are the row- or columnindex when using a Gridor the name when I have to use one or the header (e.g. when using a GroupBox).
  • I would use styles when I have a lot of styling tags like margin, padding, etc.

Maybe this helps you a bit.

Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51
2

You could refer to the these inofficial XAML coding guidelines: https://github.com/cmaneu/xaml-coding-guidelines.

The main principles are that your markup should be easy to follow and be as uncontroversial as possible :)

Paul Stovell has listed some as well: https://paulstovell.wordpress.com/2007/05/04/xaml-and-wpf-coding-guidelines/

There aren't any official coding guidlines for XAML markup though.

mm8
  • 163,881
  • 10
  • 57
  • 88