By Doumer
While building Xamarin Forms applications there are several things which 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 the Xamarin Month. The theme was code snippets for Xamarin Forms. You can find all of the snippets disused in this article here.
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 which might help you be a faster developer.
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.
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 which 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; } } |
One of the most used value converter is surely the boolean to converter. Which returns a value of 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); } } |
One other very useful piece of code is a value converter which negates boolean values. This snippet creates a value converter which 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 are very useful, but it 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.
In both cases, the snippet will generate a simple custom renderer which 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 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 which you can modify on the fly.
1 2 3 4 5 6 7 8 | [ContentProperty("Text")] public class MyExtension : IMarkupExtension { public object ProvideValue(IServiceProvider serviceProvider) { return "Simple text"; } } |
These are some very usefull Xamarin Forms snippets which you can download and install from here. Installing snippets on visualstudio is easy, here is a guide which 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
Recent Comments