If you’re developing applications in WPF or Silverlight you’ll most likely be using the property change interfaces available instead of writing complex XAML code, or you should … In the beginning you’ll copy the code fragment from an example or write it yourself, but pretty soon you’ll notice that this is a recurring piece of code that you’ll need quite often. This is where code snippets become very useful.
A colleague of me was experiencing the same problem where he needed to implement the INotifyPropertyChanged interface on quite a few classes, but he wasn’t to familiar with creating his own snippets. So together we took his code fragments and turned it into code snippets.
The notify.snippet produces the implementation of the INotifyPropertyChanged interface and is mapped to ’notify’ (start typing ’notify’ and hit the Tab key twice in Visual Studio to use the snippet).
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
The chprop.snippet provides you the VS2005 ‘prop’ snippet (private field and public accessor) with a call to the previous defined method if the property did change.
private int myVar;
public int MyProperty
{
get
{
return myVar;
}
set
{
myVar = value;
NotifyPropertyChanged("MyProperty");
}
}
This snippet is mapped to ‘chprop’ (like in changed property). If you want to alter the mappings of your snippets you can open the XML file and change the value in the Shortcut-tag to your choosen string.
<Shortcut>notify</Shortcut>
To use these snippets, simply copy both files (notify.snippet and chprop.snippet) in your code snippets directory. By default this is the “Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets” directory under your My Documents folder.