In the first part of this article, I showed different tips when creating a custom control, in this part I’ll be covering some additional tips.
As in the previous article, I’m NOT going to show you how to create a custom control (if you want to learn about that you can read this great article by the MFractor blog). My goal is to share some tips and tricks that you can apply when creating a custom control.
–
Tip 1: Use Type Converters
A Type Converter allows you to convert values from a type to a different type, this is helpful when creating bindable properties in Xamarin Forms because it saves you a lot of time and validation code.
For example:
The code shown above does the work, but what happens if we want to set the pre-defined Xamarin Forms NamedSize enum values like:
To achieve this, you just must add a TypeConverter to the property:
If you want to learn more about it, I recommend you reading this article I posted a time ago.
–
Tip 2: When using the control use Styles when is necessary
Using styles for your controls is helpful when it has many UI properties. For example, if we are creating an ExpandableParagraph Control with a lot of UI related properties:
Instead of setting them one by one in XAML, we can create a style and then apply it to the control to be able to reuse it. Also is easier to support features such as DarkMode.
Important Tip: To make it work, you need to make sure the BindableProperty name is well defined, if not you will see this error:
The issue is because the bindable property naming convention should be PropertyName + “Property”. In this case, the name is TextFontAttributessProperty, (Had an extra s) and it should be TextFontAttributesProperty since the property name is TextFontAttributes.
–
Tip 3: Do Null Checks
A crash in our custom controls can really impact our applications. Sometimes the crash log error doesn’t specify properly where is our code crashing. The fix could be as simple as adding a null check inside our control for an specific scenario that needs to handle properly a null value.
For example:
In this scenario, we have control with an AttachedProperty that updates the view property when its value changes. If we don’t do a null check here to validate if the child or parent is not null, then if the property is set while destroying the view the application will crash due to a NullReferenceException.
–
Tip 4: Use ContentProperty to specify the Content
If you are creating a Custom Control that uses an external view, you would probably add a bindable property to set the Content.
Example:
To use it:
If you want to simplify setting the Content property, you can use ContentProperty as you can see below:
To use it:
–
Tip 5: Binding Modes
“Not all the properties should be defined with the default Binding Mode”. When we are starting with Xamarin Forms one of the first things we learn is the use of Binding Modes. But even after learning that what we typically end doing is leaving the default binding mode or setting Binding Mode as TwoWay.
A time ago I read a great article by Daniel Hindrikes where he explains the importance of using BindingMode OneTime. After reading it you will start thinking about binding modes.
For example, if you are creating a Custom Control that needs a BindableProperty that will only be set in the constructor. Instead of doing this:
You should do:
That’s all for now, I hope these tips are helpful for you!
Happy coding! 🙂