While building Xamarin Forms applications there are several things that you find yourself repeating again and again. These might include creating value converters, markup extensions e.t.c. These repetitions could be made a lot easier using Xamarin Forms Snippets. And that is what we are covering today.

This article is part of my contribution to Xamarin Month. The theme was code snippets for Xamarin Forms. You can find all of the snippets disused in this article here.

Xamarin Forms Snippets

As I mentioned earlier, Xamarin Forms development can be faster with snippets available that do a large portion of our work. It is really easy to get started building snippets, here are a few snippets I made that might help you be a faster developer.

Value Converters

Value converters really come in handy, especially when you use XAML to build your layout. And you might surely be sick and tired of repeating your code for each and every value converter. Here is a simple snippet to speed this up.

Snippet Code: valconv

It generates a basic value converter and you can change the name and modify its input and return type easily. For example, here is a simple value converter that tells if the value input is null or not.

Boolean to Converter. Snippet Code: boolconv

One of the most used value converters is surely the boolean to converter. Which returns a value of a specific type depending on whether the input is true or false.

Inverter Converter: invconv

One other very useful piece of code is a value converter that negates boolean values. This snippet creates a value converter that gets an input of type bool and negates it.

If you like this post, please don’t forget to subscribe to this page’s notifications, follow me on Twitter Github and Linkedin to stay updated with new articles like my page on Facebook too.

Custom Renderers

Custom renderers are very useful, but they can be very confusing, and even for experienced Xamarin Developers, writing a custom renderer without doing a google search is not easy. Most often, when exporting the renderer (ExportRenderer). I miss-match the position of the custom view in the shared project and the actual renderer.

The iOS custom renderer’s snippet code is: icrend

The Android custom renderer’s snippet code is: acrend

In both cases, the snippet will generate a simple custom renderer that you customize on the fly. But you should Note that you will have to move the Export renderer above your namespace.

Markup Extensions

Markup extensions are another useful tool especially for those using XAML to build User interfaces. You might need several markup extensions in your app. To avoid repetition, here is a useful snippet for you. By default, the snippet creates a simple markup extension that you can modify on the fly.

Snippet Code: mx

Conclusion

These are some very useful Xamarin Forms snippets that you can download and install from here. Installing snippets on VisualStudio is easy, here is a guide that helps you through this process. You might also like this blog post about using reactive UI with Xamarin Forms.

Follow me on social media and stay updated

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.