Anatomy of Code snippets in Visual Studio for Mac

Code snippet is a shortcut that can be used to generate a code from a specific template. Example of a built-in code snippet:

Type cw and double press the Tab key will result in Console.Writeline();

Thats a pretty simple example, however we pressed only 4 keys instead of 19 (ignoring the autocompletion of IntelliSense). In more advanced cases it might be a code snippet to generate a BindableProperty or a simple property in a ViewModel that should notify the binding engine about updates. Sounds like we can increase our performance by letting the code snippets generate boring repetitive code for us.

Visual Studio for Mac is shipped with a default code snippets that can be used as a great example. Let’s take a look on the cw code snippet. First let’s open the editor by clicking on Visual Studio > Preferences > Text Editor > Code Snippets. Now select the cw code snippet under C# group:

Screenshot 2018-11-29 at 00.26.55

#1. Shortcut – is the shortcut we have to type in order to generate the code from the template. In this example it is cw (Console.WriteLine).
#2. Group – there are different available groups including F#, Python and Razor.
#3. Variables – on the screenshot #3 appear twice to demonstrate the definition of the $SystemConsoleWriteLine$ variable and its properties.
#4. Default – stands for the default value of the variable. Please note that in order to avoid confusion we should also provide a namespace.
#5. Function – we can apply built-in functions like GetSimpleTypeName("System#Console.WriteLine"). It will make sure to remove the namespace before Console.WriteLine if using System is already in place otherwise it will use the default value (#4). The list of supported functions can be found here.

The template is just a XML file that live in ~/Library/VisualStudio⁩/{version}/Snippets directory, which means that you can easily import and export code snippets that you create.

Earlier today I created a GitHub repository with a couple of handy code snippets for Visual Studio for Mac and especially for Xamarin. You are welcome to check and contribute by sharing your own code snippets!

Visual Studio for Mac tips & tricks

Switching from old good VS (for Windows) to a new cool VS for Mac can be painful. Original VS was released in 1997 (according to wikipedia) while VS for Mac was released only in 2016. Yes, it is based on XamarinStudio which is built on MonoDevelop but it still has a long way to go in order to be close to it’s ancient relative.

In this article we will take a look on VS for Mac “hidden gems” that can optimize and smooth your workflow. All you have to do is to open Mac’s VS Preferences and read this article on the side.



One of the very basic yet very important settings is the Font. While there is nothing bad using the default font, FiraCode could beautify your code and improve it’s readability by replacing sequences of characters by a single ligature.

The project repository on github contains detailed information on how to install it in your system. It’s also great because this font can be used almost everywhere.

Continue reading “Visual Studio for Mac tips & tricks”

Automatically converting PCL to .NET Standard 2.0 project

Mar-23-2018 01-41-34

Every time you create a new Xamarin.Forms project  in Visual Studio for Mac you have to manually convert it to .NET Standard. The conversion is very straightforward and can be done with just a few steps:


How to Convert a Portable Class Library to .NET Standard and Keep Git History

  • Unload your PCL project (right click -> unload), and start editing it (right -> click edit)
  • Delete Everything in the csproj and insert this:
<Project Sdk="Microsoft.NET.Sdk">

    <!--<PackageReference Include="" Version=""/>-->

  • Add back NuGets (simply open packages.config, and add the package references above, or via the NuGet package manager.
  • Delete AssemblyInfo.cs (this is now in the csproj) and packages.config (also in csproj via PackageReference)
view raw hosted with ❤ by GitHub

Hopefully one day VS team will take care of it, till then, I decided to automate this process and created an add-in/extension for VS for Mac – Mutatio.

Mutatio – in Latin means change, transformation or exchange.

Mutatio can convert newly created or existing projects. Please keep in mind that there might be NuGet packages that does not support .NET Standard 2.0, in this case you may see related exceptions.

In case you change your mind and you want to rollback, Mutatio is making a backup of all the files it modifying and deleting under the project’s root directory within mutatio_backup folder. So all you have to do is to copy the files back to your project and reload the solution.

One of the biggest challenges I met while development was related to reloading the project after conversion. Within VS for Mac after manually modifying the *.csproj under the right click menu of the project there will appear a Reload option, however I didn’t find a way to call this method programatically. Currently, the whole solution will be reloaded as a workaround. If you know how to solve the problem programatically I would really appreciate if you will share your knowledge by contributing or leaving a comment.

More details can be found on GitHub.

Most common mistakes beginners make in Xamarin.Forms

We live in a great time where technology evolves fast and we need to keep up with it if we want to stay relevant. Beside this, we also have to be productive, use the latest and greatest tools, implement the best available solutions and deliver on time. Following article mentions a list of most common mistakes we tend to do while using Xamarin.Forms.

Continue reading “Most common mistakes beginners make in Xamarin.Forms”

“DeepClean” is a must have Visual Studio extension for macOS

Yesterday I shared with you a command to recursively delete /bin, /obj and /packages directories. While this command is helpful, it might be a pain to use. Since you have to keep a terminal window opened and in general switch context from your IDE to terminal and etc.

A simple solution would be to execute this command without leaving the IDE at all, however in this case I had to extend the IDE itself. Thats how DeepClean extension was born. I am planning to write a proper post about how to extend visual studio for mac a bit later. So stay tuned.

Currently the extension does 2 simple things:
– Deleting /bin & /obj dirs
– Deleting /packages dir – after this you will have to restore the nuget packages, otherwise the solution wont build

You are welcome to download, test and contribute on github.
Please keep in mind that this extension is making it’s first steps, please make sure you have a back up of your code before using it!

Quickly delete “bin”, “obj” and “packages” directories under a .NET solution

Recently had to update some old Xamarin.Forms project to the latest and greatest XF and very quickly I realised that it is not going to be an easy task, since I had to manually manipulate the csproj files to remove the old nuget dependencies. I found myself going thru multiple projects multiple times in order to delete the “bin”, “obj” & “packages” directories to fix the miscellaneous build errors and I came up with a very simple script to recursively delete delete the “bin”, “obj” & “packages” directories:

# Recursively find and delete "bin", "obj" and "packages" dirs
# Backup your code before using this script, I am not responsible for any data loss. Please use it wisely.
find . -iname "bin" -o -iname "obj" -o -iname "packages" | xargs rm -rf

Backup your code before using this script, I am not responsible for any data loss. Please use it wisely.

To use this script: open a terminal in your solution’s root directory and copy paste the script above. Keep in mind that “bin” & “obj” directories will be regenerated after the next build, however “packages” directory will appear again only after restoring nuget packages for the solution.

Installing Xamarin components

Xamarin components are easy to install, all you have to do is to download a zip, extract the content and to reference the dlls in your project. This would work if you download the component manually from components gallery. Same thing can be done using Visual Studio in a more robust way via ‘Components’ under the targeting platform projects. Either way, the component will be downloaded and installed only for a specific project.

Sometimes it might be useful to keep the component in cache, so it will be available globally. This is where the XAM file extension comes into play. Generally speaking, it is just an archive, you can change the file extension to zip and use it in a regular way, or vice versa change zip to xam.

Regardless of your OS you have to download xamarin-component.exe.
On Windows execute the next command:

xamain.component.exe install <component.xam>

On MacOS execute the next command with the same executable:

mono xamain.component.exe install <component.xam>

If the command executed successfully, you should see the installed component:

Screen Shot 2017-11-14 at 21.20.30

Please note the “Included in this project” and “Installed on this machine”. Components under the second section will be always there unless you will clean the cache. On MacOS the components will be installed under ‘~/Library/Caches/Xamarin’. Please let me know where the cached components are stored on Windows.

Though about optimisation of work with XAML in Xamarin.Forms

What is great about Xamarin.Forms? XAML of course! Especially if you are familiar with it from WPF / Silverlight times. However, the experience with XAML in Xamarin.Forms is totally different. Unfortunately, you will not have such a great intellisense, by default you will have to discover typos in XAML at runtime, no visual editor (yet) and without preview. I have been using VS 2017 on Windows and VS For Mac on macOS, in both cases problems listed above exists.

There are a lot of threads on stackoverflow about these problems and I am repeating myself, again and again, so I decided to write a post about it. I don’t have a magic solution, just a few tricks and a though.

If you are already familiar with XAML and Xamarin.Forms and you don’t care that much about intellisence you can turn on XAML compilation to catch the typos at compile time.
You can enable it at the assembly level, by adding the next line of code to your AssemblyInfo.cs:

[assembly: XamlCompilation(XamlCompilationOptions.Compile);

Or turn XAML compilation at the class level, just the next line above class declaration:

[XamlCompilation (XamlCompilationOptions.Compile)];

More detailed information can be found here.

FYI: If you set the BindingContext inside XAML you may meet this bug.

Remember that all you do in XAML is compiled to code in the end. That means that if the IDE is not working that great with XAML for Xamarin.Forms at this time, you can write everything in plain C#!
Sounds weird, however, all the problems listed above will be solved – except preview. But I find it attractive enough to try. Defining your UI layout in code behind will not violate any of MVVM principles as far as it’s not going to include any business logic.

If you know any other tricks please share.
Have a nice week.

How to debug an iOS app build in Xamarin on a real device for free?

“With Apple’s release of Xcode 7 came an important change for all iOS and Mac developers–free provisioning.”

So all you need is an apple id and to configure your IDE.
There are a lot of guides available out there, so this post is not going to be another one:
– Xamarin Developer Guide

One thing that can be confusing is that first, you need to create a Xcode project with the same “Bundle Identifier” and then download the free provisioning profile. Just pay attention to the uniqueness of your bundle identifier otherwise, it won’t work.