My Visual Studio snippets
One of the great features of Visual Studio, that’s been around for a while is snippets. Frankly, I should build more of these because I use them *so* often.
I find them so useful I thought I’d share them here, either for you to use or to template into ones of your own.
Firs t,when building templated controls I write this a lot:
[TemplatePart(Name = ElementContent, Type = typeof(FrameworkElement))]
And it’s not just that, I then have to create a constant to define “ElementContent” and then usually load that value into a member variable as part of OnApplyTemplate.
So I built a snippet that builds this stuff out for me, resulting in this:
[TemplatePart(Name = ElementContent, Type = typeof(FrameworkElement))]
//private const string ElementContent = "Content_FrameworkElement";
//private FrameworkElement _Content;
//_Content = (FrameworkElement)GetTemplatePart(ElementContent);
Which gives me some extra code commented below that I can just copy down into the class. Nice!
Building snippets is easy, it’s just an XML format. It works great, just add the folder to VS via the Snippet Manager (Tools –> Code Snippets Manager).
In my case, I have a snippets folder on Live Mesh that gets replicated to all the places I write code, so I always have my snippets.
For the snippet above, the xml looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="https://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Template Part Declaration</Title>
<Shortcut>template_part</Shortcut>
<Description>Template Part Decl</Description>
<Author>Shawn Burke</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>name</ID>
<ToolTip>Template Part Name</ToolTip>
<Default>PartName</Default>
</Literal>
<Literal>
<ID>type</ID>
<ToolTip>Element Type</ToolTip>
<Default>type</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[
[TemplatePart(Name = Element$name$, Type = typeof($type$))]
//private const string Element$name$ = "$name$_$type$";
//private $type$ _$name$;
//_$name$ = ($type$)GetTemplatePart(Element$name$);
$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
It’s pretty simple, I’ve highlighted the interesting parts. There are much fancier things you can do with snippets which I should probably learn.
Here’s a list of the ones I use regularly, and a zip of all of them attached to this post.
- Create an INotifyPropertyChanged class implementation – inpc_impl
- Create a property that raises a INotifyPropertyChange.PropertyChanged event – inpc_prop (I probably use this one the most)
- Create a Silverlight/Phone formatted Dependency Property with a change handler – propdpsl
- Create a template part attiibute (above) – template_part
- Create a template visual state attribute – template_visualstate
- Create a wrapper for an async call to an Service Reference proxy – svca
The last one generates this code, for a method called GetComments, which gives me nice lambda handlers for success and error cases, which is the pattern I use for most calls to a generated proxy.
public void GetComments(string postId, Action<IEnumerable<Comment>> success, Action<Exception> error) {
var ws = new BlogServiceClient();
ws.GetCommentsCompleted += (s, a) =>
{
if (a.Error != null) {
if (error != null) {
error(a.Error);
}
else {
HandleError(ws, "GetComments", a.Error);
}
}
else {
if (success != null) {
success(a.Result);
}
}
ws.Close();
};
ws.GetCommentsAsync(postId);
}
The highlighted parts are template parameters.
Comments
Anonymous
April 02, 2011
I am unsure if i understoöd your article. If i am not mistaken a snippet is bit of code which can be defined by using xml and can be inserted into the code by somehow loading it. Could you elaborate on how to fire up / activating a snippet? Thanks a lotAnonymous
April 02, 2011
Hi, Sure, good question. Snippets work by typing the shortcut, which you will see in Intellisense. When you select it by hitting Enter, you'll be able to fill in the various fields. Visual Studio has a bunch of standard snippets. Try typing "prop" for example, to create a standard property. In the case of these custom ones, the short cuts are inpc_impl, inpc_prop, propdpsl, etc, as noted after the description. Hope that helps, ShawnAnonymous
April 11, 2011
Nice article. I completely agree, snippets are very valuable. I would, however, recommend downloading Snippet Designer for Visual Studio. You can find it in the extension gallery. It really makes it alot easier to create snippets, as you don't have to mess with the XML.