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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class IsNullConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { bool returenedValue = string.IsNullOrEmpty(value?.ToString()) && value == null; return !returenedValue; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return value; } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class BooleanToConverter<T> : IValueConverter { public T TrueValue { get; set; } public T FalseValue { get; set; } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value.GetType() != typeof(bool)) return FalseValue; return (bool)value ? TrueValue : FalseValue; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if (value.GetType() != typeof(T)) return false; return ((T)value).Equals(TrueValue); } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | [assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))] namespace Sample.Droid { public class CustomEntryRenderer : EntryRenderer { public CustomEntryRenderer(Context context) : base(context) { } protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) { base.OnElementChanged(e); } protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); } } } |
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
1 2 3 4 5 6 7 8 | [ContentProperty("Text")] public class MyExtension : IMarkupExtension { public object ProvideValue(IServiceProvider serviceProvider) { return "Simple text"; } } |
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