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.

image

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.

snippets.zip