Adding snippets to Xamarin Studio – example with MvvmCross property changed

Pre:

Read below to learn how to add snippet to Xamarin Studio to generate MvvmCross viewmodel property changed code.

We know your first feelings about Xamarin Studio after using Visual Studio with ReSharper only (btw, where did “Sort and Remove usings” go?).

We miss, for example, ability to generate your code using snippet.

You type vmpc and you then get a full blown property changed code with just several key presses. Time saved for business logic, yey!

Until now, I thought it’s not possible with Xamarin Studio, but it is.

 

Here is how to add your custom Xamarin Studio snippet (and later share with other developers):

1. Open menu  Xamarin Studio -> Preferences.

Screenshot 2015-03-14 13.05.10

2. Go to section Text Editor – Code templates.

Screenshot 2015-03-14 12.46.54

3. Press “Add button” and fill out all the properties including snippet template.

Screenshot 2015-03-14 12.47.34

 

$variable$ here stands for your template variable, you can have as many as you want.

$selection$ is the code that is selected at the time snippet is applied (so you can wrap selected code with your snippet).

$end$, probably, stands for the last few lines of selection or code right after

4. Customize your template variables properties. Right part of this window is actually a dropdown with auto detected variables from your template. Choose one and customize it.

For example, for type variables you can specify default type like “string”. So that you have less to type next time.

Screenshot 2015-03-14 12.47.45

5. Save and enjoy by just typing snippet shortcut right in the code:

Screenshot 2015-03-14 13.07.30Screenshot 2015-03-14 13.10.55

Great that we have opportunity to customize and add snippets. Really helpful from Xamarin team would be to get some more information on what is possible and also list what functions you can use in the templates (like making template variables lower case, etc).

 

Here is how to share snippet with other developers:

Your snippets are saved into xml file, each one in it’s own.

In order to share it, go to folder ~/Library/XamarinStudio-5.0/Snippets

Screenshot 2015-03-14 13.13.03

Screenshot 2015-03-14 13.13.43

 

Screenshot 2015-03-14 13.14.31

Then you can upload the file to https://gist.github.com and other people can just download that file, put it to the same folder and restart Xamarin Studio.

How do I get the snippet?

1. Download MvvmCross Property Changed snippet for Xamarin Studio.

2. Copy to ~/Library/XamarinStudio-5.0/Snippets and restart Xamarin Studio.

Please share your snippets and your ideas or knowledge regarding this topic.