Xamarin.Forms Intro Part 2 – Build a Custom Control Library

UPDATE: In my example I use the namespace Xamarin.Forms, this is probably not the best namespace to use, it’s better to use CompanyName.Forms or Community.Forms.

This is the second part in a two part series introduction Xamarin.Forms. In the first part we demonstrated a basic app with Xamarin.Forms. In this part we will create a custom control library.

The library has four projects, the common PCL with references to Xamarin.Forms and a project for each of the platforms with references to Xamarin.Forms and Xamarin.Forms.Platform.(platform name), as per screenshot below.

Screen Shot 2014-06-03 at 9.47.21 pm


All the projects each have single important class, the PCL has a View class which in my case is the CalendarView while the platforms have a ViewRenderer which in this case is CalendarViewRenderer.

Xamarin.Forms.Calendar (Portable) – CalendarView.cs

Just to understand how it works from a consumer perspective, this  class is referenced from the calling PCL (eg the CalendarViewSample). It’s the only class you have to worry about as a consumer of the library. The renderers are called by the platform.

Xamarin.Forms.Calendar.Droid – CalendarViewRenderer.cs

In the class above I just override the OnModelChanged method and then in that method I create a UI element. Then I wire up the OnDateSelected event and call the event on the View(which in turn goes into the consumers PCL). Finally I call SetNativeControl that add the view onto the page.

You’ll also notice the ExportRenderer line which is a assembly attribute and it links the View and the Renderers. All the other platforms are similar to this.

Xamarin.Forms.Calendar.iOS – CalendarViewRenderer.cs

Xamarin.Forms.Calendar.WP – CalendarViewRenderer.cs

So that’s it, creating custom controls for the Xamarin.Forms platform is pretty simple. I hope others can also enjoy making their own controls for the platform, I look forward to seeing what the community can create.

There source code for this project available on github: https://github.com/rid00z/Xamarin.Forms.Calendar

I’ve also done a little demo video, see below:


10 Responses

  1. Is it possible to make a custom control with multiple controls as building blocks?

    For instance, I’m wanting to build a custom spinner renderer so I can control the look and feel of it across all platforms. To do that I’m going to need both the picker object and an entry field. I was looking into just using a custom renderer, but it seems I will need to write my own spinner on android.

    I’m both new to the platform and it seems the Xamarin.Forms package is still fairly new.

  2. Would you be updating the sample for the lastest Xamarin.Forms version. Looks like there is no NativeRenderer(atleast on Android) anymore, just ViewRenderer, Having issues with SetNativeControl() on that one.

    Thanks in advance!

    1. Hi Sarthak

      If you manage to get it working can you please submit a pull request?



  3. NativeRenderer appears to be missing in the newer versions.Can u please send me the working project of Xamarin.Forms.Calendar.IOS.

Leave a Reply