Visual Studio 2010 Snippets for MVVM
A couple of weeks ago, I looked up how to create your own snippets in Visual Studio 2010 and was pleasantly surprised at how easy it was. I had grown tired of typing out / copy & pasting code in my WPF / MVVM project for observable properties and RelayCommands (which already cuts out a lot of redundant code) and so created two rudimentary snippets.
The first, whose trigger is oprop, is for observable properties. These are simply properties that have a private backing field and call OnPropertyChanged in the setter. Example:
1 2 | private int _prop; public int Prop { get { return _prop; } set { _prop = value; OnPropertyChanged(“Prop”); } } |
The second, whose trigger is cmd, is for RelayCommands. I took a few stylistic liberties with this one, so it may not be as much use to you as it is to me. Example:
1 2 3 4 5 | private RelayCommand _nameCommand; public ICommand NameCommand { get { return _ nameCommand ?? (_nameCommand = new RelayCommand(param => Name())); } } |
I went ahead and zipped up the two .snippet files, but WordPress strictly prohibits uploading such things so I’ve added “.txt” to the extension. To use them, download the file, rename to “snippets.zip,” uncompress the file, and then add the two separate files to your snippets directory. Feel free to open the snippets themselves and tweak to your liking.
EDIT: WordPress added an additional “.zip” extension to the file- how bizarre. Directions are the same though- just rename to “snippets.zip” post-download.