You are on page 1of 6

6 useful Xamarin Forms Snippets - Cool Coders about:reader?url=https://doumer.

me/5-useful-xamarin-forms-snippets/

doumer.me

6 useful Xamarin Forms Snippets -


Cool Coders
5-6 minutes

.Net , C# , Xamarin , Xamarin.Forms ,

21 Jun

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.

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 which might help you be a faster developer.

Value Converters

1 of 6 11/28/2020, 4:12 PM
6 useful Xamarin Forms Snippets - Cool Coders about:reader?url=https://doumer.me/5-useful-xamarin-forms-snippets/

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 which tells if the value input is
null or not.

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

2 of 6 11/28/2020, 4:12 PM
6 useful Xamarin Forms Snippets - Cool Coders about:reader?url=https://doumer.me/5-useful-xamarin-forms-snippets/

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 public class BooleanToConverter<T> : IValueConverter

2 {

3 public T TrueValue { get; set; }

4 public T FalseValue { get; set; }

5 public object Convert(object value, Type targetType,

6 object parameter, CultureInfo culture)

{
7

8 if (value.GetType() != typeof(bool))

return FalseValue;
9

10 return (bool)value ? TrueValue : FalseValue;

}
11

12 public object ConvertBack(object value, Type


targetType, object parameter, CultureInfo culture)
13
{
14
if (value.GetType() != typeof(T))
15
return false;
16
return ((T)value).Equals(TrueValue);
17
}
18
}
19

20

21

3 of 6 11/28/2020, 4:12 PM
6 useful Xamarin Forms Snippets - Cool Coders about:reader?url=https://doumer.me/5-useful-xamarin-forms-snippets/

22

Inverter Converter: invconv

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

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.

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 which you customize on the fly. But you should Note
that, you will have to move the Export renderer above your
namespace.

1 [assembly: ExportRenderer(typeof(CustomEntry),
typeof(CustomEntryRenderer))]
2

4 of 6 11/28/2020, 4:12 PM
6 useful Xamarin Forms Snippets - Cool Coders about:reader?url=https://doumer.me/5-useful-xamarin-forms-snippets/

3 namespace Sample.Droid

4 {

5 public class CustomEntryRenderer : EntryRenderer

6 {

7 public CustomEntryRenderer(Context context) :

8 base(context)

{
9

10 }

protected override void


11
OnElementChanged(ElementChangedEventArgs<Entry>
12
e)
13
{
14
base.OnElementChanged(e);
15
}
16
protected override void
17 OnElementPropertyChanged(object sender,

18 PropertyChangedEventArgs e)

19 {

20 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

5 of 6 11/28/2020, 4:12 PM
6 useful Xamarin Forms Snippets - Cool Coders about:reader?url=https://doumer.me/5-useful-xamarin-forms-snippets/

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.

Snippet Code: mx

[ContentProperty("Text")]

public class MyExtension : IMarkupExtension

public object ProvideValue(IServiceProvider


serviceProvider)

return "Simple text";

Conclusion

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

6 of 6 11/28/2020, 4:12 PM

You might also like