In part one I discussed the
Tools/Options/Text Editor/XAML/Miscellaneous section.
In part two I look at the XAML Formatting section. The XAML editor in Visual Studio 2008 is much improved over the 2005 Extensions version.
Developers are a picky bunch. I can spend 30 minutes arranging my code/XML/html/xaml to my liking, getting the spacing just right, indenting each line so that my code is a work of art. You could look at my code and five seconds later you’d be wishing that you could reformat it to your coding standards. Some of us like our tools to auto-arrange our text, some of us go ballistic if the tool so much as moves a semi-colon or curly brace.
In VS 2008 you can specify whether you want your XAML rearranged automatically or whether you’d like the XAML left alone unless you intentionally ask for an auto-format.
The Formatting/General screen is fairly self explanatory.
The following three events can trigger auto-formatting.
- On completion of end tag or simple tag
Formatting occurs when you finish typing the end tag. It can also happen when typing a simple tag. Simple tag is Micro-speak for a tag with no attributes (example <ListBox />).
- On completion of start tag
Formatting occurs when you finish typing the element start tag.
- On paste from clipboard
Formatting is triggered whenever you paste XAML from the clipboard into the XAML view.
Auto Delimiters with ‘Quotation Mark Style’
Well formed XML requires any XML attribute value to be delimited with either double quotes or single quotes. Select your preference here. I prefer single quotes when typing as I don’t have to use shift key.
Visual Studio has two settings that affect the length of text allowed on each line in the editor. The first choice, Word Wrap, is part of the the Text Editor General section. When Word Wrap is enabled your text flows to the next line whenever the text is wider than the width of the edit window. Just like Microsoft Word.
In the following example lines 2 and 10 are wrapped.
Tag Wrapping is similar to Word Wrap but it happens only when your XAML is auto-formatted by Visual Studio. You specify the maximum number of characters per line and VS arranges your tags accordingly. Confusingly the Tag Wrap only happens when you add your elements using the Designer and not the XAML editor.
On the Spacing screen we choose how we want our attributes and elements arranged on the page.
- Preserve newlines and spaces between attributes
- Insert a single space between attributes
- Position each attribute on a separate line
The element is split across many lines. Each attribute occupies its own line. This is the setting I use.
Note: if you move the attribute on the first line to a new horizontal position and trigger an auto-format (Ctrl-F,Ctrl-D) the subsequent lines will align with the first attribute position.
- Position first attribute on same line as start tag
When unchecked, attributes are not allowed on the same line as the start tag.
The following three values control what happens to blank lines that are between lines of XAML text. I think you can figure out what they do.
To follow along with this example you need to have the final release version of Visual Studio 2008 installed. Find trial versions here.