Bibbidi-Bobbidi-Boo – Using a Custom Renderer to enchant your UI

Bibbidi-Bobbidi-Boo – Using a Custom Renderer to enchant your UI

While Xamarin.Forms provides a unified way to also create our UI from a single code base, the default controls tend to be just that; default controls. To be able to customize these, you can use a custom renderer.
Don’t get me wrong, I am very happy with these controls and they can be used to create simple, no-fancy-design-needed apps. But when you have created some apps, you want a little bit more. You have to stand out between all the other apps already out there and how to do that better then with an awesome UI?

Luckily for us, Xamarin already foresaw this and gave us something that is called Custom Renderers (documentation). With custom renderers we’re able to render controls in our own way, rather than having Xamarin rendering it to the default control for that platform. With this we can customise how an image looks for example. And this is exactly what we will be doing, turn our UI into something magical like we are the fairy godmother herself!

If you’d like to see this work in a moving picture, check out my vlog episode on custom renderers.

The ugly pumpkin

Right now my 4DotNet app has the default implementation of a ListView with Image Cells. This shows up in the app like this:

Employee list screenshot iOS
Employee list screenshot iOS

It looks ok! But why settle for ok if we can have AWESOME?
For mobile apps, especially for profile pictures, it seems to be required that they are shown as circular images rather than the squares I have now. Let’s see if we can turn this around.

Bibbidi-Bobbidi-Boo! Custom Renderer I choose you!

Now let us work some magic.

To work with our custom renderer, we first have to create our own image type. Let’s call this CircularImage.
This type we can use in all our apps, so put it in the shared project of your solution.

Our CircularImage inherits from the default Xamarin.Forms.Image. If you like you could add some custom properties and other stuff in there, but for what we’ll be doing right now that won’t be necessary.

Now the rest consists of creating a custom renderer per platform. These I will name the CircularImageRenderer and these will inherit from the default Xamarin.Forms ImageRenderer, which will be the platform specific one (i.e. Xamarin.Forms.Platforms.iOS.ImageRenderer).

Bibbidi-Bobbidi-iOS!

Let’s start with iOS.

We create our new file according to the above empty class and declare our iOS CircularImageRenderer.

We start with defining a custom private method which transforms our square image to a round one and adds a border around it so it looks extra fancy.

In itself this does nothing, because we have to override our default renderer behaviour to make this transformation visible. So we override the OnElementChanged and OnElementPropertyChanged method like underneath.

With this code we respond to the event which is fired when the Element (Image in this case) has changed or any property of the Element has changed.

Within the ElementChanged we just check for nulls and then draw our CircularImage, with the PropertyChanged we check if the changed property has anything to do with the appearance of our Image. If so, we then redraw, if not, we do nothing.

That concludes the code we have to write for our renderer, that wasn’t so bad now, was it?

There is one vital line we have to add. We have to tell Xamarin that whenever it finds our custom control CircularImage, that there is a custom renderer for that. We do that in the same way as we saw in my previous post about Dependency Injection.

Note the first line here. We state that we export a renderer for our type CircularImage, and that renderer is of our type CircularImageRenderer.

Bibbidi-Bobbidi-Android!

On to our Android renderer.

As you might expect this process will be roughly the same, only the implementation will be different. However, this is more true for Windows Phone than Android. On Android there is no physical layer to interact with, so instead we need to override the method that Xamarin uses to draw the child controls.

We can do this by using the below code;

Also, since API Level 18, hardware acceleration for drawing was introduced. As you might know, that is something you’d want to use!

You can set a flag to use software rendering for below API Level 18 in the OnElementChanged event like below.

Of course, don’t forget to export your renderer!

Bibbidi-Bobbidi-Windows Phone!

The Windows Phone renderer is again a bit easier. We can just use the Clip property here and assign a EllipseGeometry to it, like so.

And again, let’s not forget to register our custom renderer.

Turning it into majestic golden carriage

For the last bit we have to use our new CircularImage and see how all this looks like.

Before I have used a ListView with an ImageCell, in XAML that looks like this.

Of course an ImageCell is a composite of a few controls already which acts as a template for a list item. although you can create your own custom cell, for now I will create my own list item template right here in the ListView. This probably isn’t what you would want in your final application, but for the sake of this example it will do.

In the underneath code you will see how I replaced the ImageCell with my own template and made it look similar.

You will notice that you have to specify a new namespace which specifies where my custom CircularImage resides. In this case I have used ‘controls’.
Now with this new namespace I can define my CircularImage control just like any other ‘normal’ control.

When we run this on all of the different platforms, you will see an enchanting result…!

CircularImage on all platforms
CircularImage on all platforms

To create these custom renderers I have followed another blog-post by James Montemagno and the documentation of Xamarin, so code similarities might be apparent. All credits for the math-hocus-pocus goes to him/them.