This tutorial looks at helpful settings for the editor, that help you be more productive when reading and writing XAML.
- [Instructor] I have some XAML-specific settings that I always set in any copy of Visual Studio, and I'd like to share those with you. I'll start by looking at a couple things I can do in the editor, here. One is, I always like to have line numbers down the sides, so when I'm talking to my teammates, or when I'm writing code, or doing a course like this, I can talk about line 16 and you'll all know what I'm talking about. The other thing is, you'll see, up here in this line, that I can't read the entire line of text. Now, I know some of you don't mind scrolling back and forth, so you can scroll over here to see that, but I prefer to have my tool do the work of bringing all of my code into view.
So, I'll use something called word wrap. Now go to tools, options, then click on the text editor section, and then go to the XAML section, or I could have just typed the word XAML in here. I'll start by looking in the general section. In this section, I'll enable line numbers and I'll also enable word wrap, and also, make sure it has this default value, show visual glyphs for word wrap. Now I've got my line numbers down the side, and you'll see that line eight is wrapping down so I can read everything, and the visual glyph they're referring to is this blue arrow.
It tells me that not everything fit on line eight. Next setting is in the miscellaneous section. These are the default settings, and I suggest you leave all of these on. Closing tags, that means when you type an opening tag, like the button tag, Visual Studio will automatically add the closing tag. When you type in an attribute, it'll automatically put the quotes in. It also can do things like finish putting in your closing curly braces in markup extensions, so what do these mean? Go back over here and I'll show you.
Go down here, and I'll put in a text block, press Tab key. Now when I close the angle bracket here, you'll see it puts in the end tag. When I go to do an attribute, like, say, the width attribute, I'll type in wi, and then I'll press the Tab key. Visual Studio puts the equal sign and then two single quotes, and puts my cursor between the two single quotes. Some developers prefer to use single quotes, some developers prefer to use double quotes. Visual Studio will let you do either. I prefer single quotes, so that's the setting I've chosen in Visual Studio.
Here's how you change that. Go to formatting, and choose double quotes or single quotes. Let's talk about tabs. I like to see as much text on the screen as possible. That's why I use word wrap, I don't have to scroll back and forth, and another thing that I like to do is change my tab size and my indent size and make them smaller. That means I can see more text on my screen, so I always use the value two for my tab and indent sizes, and that means that when you have this button here, and I tab over, press Tab, it only goes over two spaces.
That means I can see more text. I still have the indent to guide my eyes, but I get more information on the screen. And, of course, if I wasn't doing a course like this, I'd also make the font smaller so I can get more on the screen, and one way you can do that in Visual Studio... Keep this at 80%, remember that. Is I can hold down the control key and use my mouse wheel to make it smaller. So, if I was developing and not teaching a course, I'd probably use a setting more like this size. I'll type in 80 to get back to the previous zoom size.
Have to fix this XAML, here, so I don't have any error. Now we'll look in the formatting section again. If you pop open this node, you'll see there's a section called spacing, and I want to talk about the attribute spacing and element spacing. Choose the first item for each one of these items. So, attribute spacing, preserve new lines and spaces between attributes, and preserve new lines in content. So, what that means is, if I add several empty lines between the grid and the button, and I do something like this...
Having trouble getting that to go up to the next line. There it goes. So, I've got two of my attributes on the same line, I've got a bunch of empty space, here. So now, if I go over to edit, advanced, and choose this, format document, which will go through my XML document and rearrange it according to the rules that I've provided in my tools options, it doesn't touch that space, and it doesn't touch my arrangement, here, on the attributes.
If I choose insert a single space between attributes, and run that same command, I can also do a Ctrl+K, Ctrl+D. Now that you see it puts a single space between them, so it removed the extra spaces, there, so everything is all collapsed next to each other, and one space in between each of the attribute values, and you see that, with the word wrap on, I can see they're all really on a single line. My favorite setting, the one I always use, is this last one, position each attribute on a separate line.
Now, I'll use that keystroke, Ctrl+K, Ctrl+D, and I think this is much more readable. Each attribute is on a single line, and each one of them's nested below the one above it. The last setting, which I don't like, but I'll show it to you, is I can also uncheck this, position first attribute on the same line. Now when I do my formatting, Ctrl+K, Ctrl+D, you'll see that it moves the first attribute down to the next line, and puts it two spaces over.
I prefer to have the first attribute on the same line as the element, that's my setting I prefer. So, we'll put this back in here. Now, the last thing I want to talk about is the element spacing. Right now, it's not touching my element spacing. I can have it remove all my empty lines. Ctrl+K, Ctrl+D. Now it's removed every single empty line in my document. What I prefer to do is, if I put lines like this in here, I prefer it to leave at least one line in there, so I choose this last setting, collapse multiple empty lines in content into a single line.
Format my document. Because I had multiple lines here, it left a single line. If I had no lines there, like this, then it would have left them alone. It won't put an extra space in there. So, those are my favorite settings. Of course, you can choose whatever settings you want, but those are the settings I'll be using as I go forward in this course.
- Recognize the pluses of choosing Windows Presentation Foundations over other Microsoft UI frameworks.
- Evaluate the different options available with Windows Presentation Foundation project types.
- Devise a Windows Presentation Foundation project in Visual Studio.
- Break down the assemblies and parts of Windows Presentation Foundations.
- Use XAML editor and editor settings.
- Selecting data binding to present data.
- Compiling interaction code for an application.
- Use control templates, 3D parts, and effects in Windows Presentation Foundations.