How code snippets in VS for Mac can help you create Xamarin Forms apps

Hello, my peeps. In today’s post, I’m going to show how to create and use code snippets on VS for Mac. This is also part of Louis Matos’ Xamarin Month for which the theme is code snippets. Go and check out his blog if you want to get more content on this topic, in there you will find a list of all the participating authors and their own articles.

What is a code snippet?

Code snippets are reusable blocks of code that can be quickly added by using a given keyboard shortcut or as an option in a context menu. These blocks tend to be very frequently used code, like if/else statements, property declarations, or even more complex code, like a class or method.

How to create a code snippet on VS for Mac?

VS for Mac already comes with a variety of default snippets for different languages, but we can also add some to help us with whatever your day-to-day coding need is. In order to add a new snippet go to Visual Studio > Preferences > Text Editor > Code Snippets. Here we can view a list of all available snippets, options to add, edit, remove and view a code snippet.

Code Snippets wizard in VS for Mac’s preferences

Opting to add or edit a snippet opens a dialog with a couple of options we can use to set up the snippet’s template.

Template configuration dialog
  • Shortcut: the shortcut used to call the snippet;
  • Group: groups the snippets on the context menu;
  • Description: an explanation of the snippet’s function
  • Mime: defines which file types will recognize the snippet’s shortcut
  • Is expandable template: indicates whether the snippet can be added to the cursor when typing its shortcut;
  • Is surround with template: allows the snippet to be shown in the Surround with context menu option;
  • Template text: the actual code snippet to be added. In here it’s possible to define keywords by setting them between dollar signs ($), as in $type$;
  • Keyword property panel: panel located on the right side of the dialog. In this example, we can see the property “default” for the “type” keyword be defined as “object”.

As we can see, creating a code snippet is not too complicated. You can also share your snippets with your coworkers or the community. To do this, simply make it available as an XML file. The snippet files are located under ~/Library/VisualStudio/{Versão}/Snippets. My VS is currently at version 8.6, but my snippets are located under an 8.0 version directory.

My partners at BurgerMonkeys and I have made a repository available at Github where you can find some snippets that have been making our lives much easier.

How can Code Snippets help to create Xamarin Forms apps?

Think about some code you use really often during your app creation process, no matter how simple it can be, that always repeats the same structure. Well, that is a very good candidate to become a code snippet.

A personal case of mine is the creation of properties with backing field to bind some controller from the View to a ViewModel property. In this snippet, I’ve set a structure that creates the property with the data type, field name and property name as parameters just by typing “propfull”, which is much faster than the manual process, even compared to the basic help VS gives with properties.

private string _name;
public string Name
{
get => _name;
set => SetProperty(ref _name, value);
}
view raw propfull.cs hosted with ❤ by GitHub

Another good example is the creation of async methods. By using the “patm” shortcut, the basic async method structure is created with the method’s name as a parameter, which is also quicker than just doing it manually.

private async Task MethodAsync()
{
}
view raw patm.cs hosted with ❤ by GitHub

I believe this could show you how we can save time with the wizardry of code snippets in our projects. The shown snippets can be found in our GitHub repository along with a few others. Feel free to use them.

I’ll see you in my next post. Cheers.

Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. Sair /  Alterar )

Foto do Google

Você está comentando utilizando sua conta Google. Sair /  Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair /  Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair /  Alterar )

Conectando a %s

Este site utiliza o Akismet para reduzir spam. Saiba como seus dados em comentários são processados.