Is there an accepted style for indenting attributes in XAML?
I have seen both of these forms:
Style #1
<TextBox
Name="someTextBox"
Width="50"
Height="60" >
Some Text
</TextBox>
Advantages
- Consistent indentation level for all elements. You won't see
sibling1
's attributes indented 8 spaces, whilesibling2
's attributes are indented 9 spaces. - Minimal indentation level. Long element names won't have extremely indented attributes.
Disadvantages
- Hard to distinguish content from attributes.
- Takes an extra line.
Style #2
<TextBox Name="someTextBox"
Height="60"
Width="50" >
Some Text
</TextBox>
Advantages
- Easy to di开发者_运维技巧stinguish content from attributes
- Saves a line of text
Disadvantages
- Different elements at the same level in the tree can have different indentation levels for attributes.
- Indentation levels can get quite high.
In short, both work (and both are supported by Visual Studio). I lean towards style #1, but the most important thing would be that I am consistent with other developers. Is one of these styles more commonly used?
I don't think there is an accepted style.
But since the structure of XAML is very different from the structure of normal imperative code, I think a different approach might be better.
I prefer a style you didn't even mention: usualy have all properties on one line. If that line gets too long, split it, preferably in a way that makes sense. So, for example, something like:
<TextBox Name="someTextBox"
Width="50" Height="60">
Some Text
</TextBox>
I don't think high indentation levels are problematic by themselves (they are in normal programming, but that's something else).
Personally, I prefer having all properties in one line of the XAML, just when I write HTML or XML. I think this may be due to the fact that I always have word wrapping enabled in Visual Studio, and have trained my eyes to read it.
Looking at both of the styles you have provided, they are actually harder for me to read.
If I had to choose though, I would pick style 2 for its readability.
精彩评论