Random programming things I'd want to remember

Tuesday, March 18, 2014

Code snippet file for a property that calls for OnPropertyChanged event

With minimal changes, this snippet will also work for PropertyChanged event. How to use:
  1. Save the following contents into a .snippet file
  2. Visual Studio -> Tools -> Code Snippets Manager (or Ctrl+K, Ctrl+B)
  3. Import, find your file
  4. While working on another ViewModel, type in propopc and enjoy

Note to self: while working on the next snippet, if variables don't show up in the result, make sure that they are included in the <Snippet> section

Helpful resource 1, helpful resource 2.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Property with a call to OnPropertyChanged</Title>
      <Author>yevgeller</Author>
      <Description>Property calling OnPropertyChanged method</Description>
      <Shortcut>propopc</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>var</ID>
          <ToolTip>Replace with a private variable.</ToolTip>
          <Default>_privateVar</Default>
        </Literal>
        <Literal>
          <ID>type</ID>
          <ToolTip>Replace with a property type.</ToolTip>
          <Default>string</Default>
        </Literal>
        <Literal>
          <ID>propName</ID>
          <ToolTip>Replace with a property name.</ToolTip>
          <Default>MyProperty</Default>
        </Literal>
      </Declarations>
      <Code Language="csharp">
        <![CDATA[
  private $type$ $var$;
         public $type$ $propName$
         {
              get { return $var$; }
              set
              {
                  if ($var$ == value) return;
                  $var$ = value;
                  OnPropertyChanged("$propName$");
              }
         }
      ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

No comments: