What’s in YOUR snippet picker?

I have recently been adding new, custom snippets to quickly stub out stuff when developing in the MVVM pattern.  Pattern programming is a great idea, but spending hours hand-typing ICommand implementations or notification properties can make you want to disavow patterns entirely and just shove it all in the code-behind. (For those of you who aren’t super familiar with MVVM, that was a joke; it’s a bad idea).

Because I am not just a soulless code monkey (and because I’m selfish and want to be able to find my snippets later if my office burns down), I have provided some of my favorite ones here.  They are provided under the GSD licence (get stuff done) so feel free to use them yourself as long as doing so will help you feign some sort of productivity.

Here be snippets:

ICommand: cmd

<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>cmd</Title>
    <Author>Steve Konves</Author>
	<Shortcut>cmd</Shortcut>
    <Description>Creates a bindable ICommand</Description>
    <SnippetTypes>
      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal>
        <ID>name</ID>
        <Default>DoWork</Default>
      </Literal>
    </Declarations>
    <Code Language="CSharp">
      <![CDATA[#region $name$
        /// <summary>
        /// Executes the $name$ command.  Supports command binding.
        /// </summary>
        public void $name$()
        {
            if (Can$name$)
            {
                // TODO: Add functionality here.
            }
        }

        #region (Can$name$)

        /// <summary>
        /// Gets or sets a value indicating whether the $name$ command can be called.
        /// Also determines whether a button bound to this command is enabled.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if the $name$ command can be called; otherwise, <c>false</c>.
        /// </value>
        public bool Can$name$
        {
            get
            {
                // Note: Consider adding additional logic here.
                return _Can$name$;
            }
            set
            {
                if (_Can$name$ != value)
                {
                    _Can$name$ = value;
                    RaisePropertyChanged("Can$name$");
                }
            }
        }
        private bool _Can$name$;
        #endregion

        #region ($name$Command)
        /// <summary>
        /// Gets or sets the $name$ command.  Supports binding.
        /// </summary>
        public ICommand $name$Command
        {
            get
            {
                return _$name$Command;
            }
            set
            {
                if (_$name$Command != value)
                {
                    _$name$Command = value;
                    RaisePropertyChanged("$name$Command");
                }
            }
        }
        private ICommand _$name$Command = new Helpers.Command { Method = "$name$" };
        #endregion

        #endregion]]>
    </Code>
  </Snippet>
</CodeSnippet>

Notification Property: propn

<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>Notification Property</Title>
    <Author>Steve Konves</Author>
    <Shortcut>propn</Shortcut>
    <Description>Creates a property which implements a backing field a property change notification</Description>
    <SnippetTypes>
      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal>
        <ID>name</ID>
        <Default>MyProperty</Default>
      </Literal>
      <Literal>
        <ID>type</ID>
        <Default>int</Default>
      </Literal>
    </Declarations>
    <Code Language="CSharp">
      <![CDATA[#region $name$

        public $type$ $name$
        {
            get { return _$name$; }
            set
            {
                if (_$name$ != value)
                {
                    _$name$ = value;
                    OnPropertyChanged("$name$");$end$
                }
            }
        }
        private $type$ _$name$;

        #endregion]]>
    </Code>
  </Snippet>
</CodeSnippet>
Posted on November 9, 2011 at 7:40 am by Steve Konves · Permalink
In: C#, Development, Silverlight, Visual Studio

One Response

Subscribe to comments via RSS

  1. Written by Chrissy Kosco
    on January 6, 2012 at 8:57 am
    Permalink

    Nice!

Subscribe to comments via RSS

Leave a Reply