Corrado's Blog 2.0

Online thoughts of a technology funatic

UserControls in Xamarin Forms

While Xamarin Forms have a rich set of controls (a.k.a. Views in Xamarin documentation) sometimes you need to create something that is an aggregate of existing controls plus custom logic. For this requirement the Windows platform provides you a UserControl that is a surface where you can drag the controls that make up the UI and write related logic within it.
This seems to lack on Xamarin Forms at the moment.

Well, that’s not true, since you can easily create UserControls your own in Xamarin Forms, let’s see how with an example (see below)

image image

Lat’s say I need to populate a list of Entries whose content has to be validated and if not valid a feedback has to be provided to the user. Since we don’t like to repeat ourselves and since this might be a requirement we might also have in future I decided to create a custom control that contains all the validation logic and exposes a set of properties that let me customize the runtime behavior of the control.

Here’s the MainView’s XAML of the previous screenshots:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns=""

 <StackLayout Padding="10" >
  <Label Text="UserControls in action!" x:Name="MyLabel" Font="Bold,Large"  />
  <c:SmartEntry Header="Enter Username (min 3, max 8 chars)"
         InvalidText="Sorry, username is not valid"
         Text="{Binding Username, Mode=TwoWay}"
  <c:SmartEntry Header="Enter Email"
         InvalidText="Whoops! invalid email..."
         Text="{Binding Email, Mode=TwoWay}"

  <Label Font="Large" Text="{Binding Info}" TextColor="Green" />


I’m sure you noted the SmartEntry component that is our UserControl, let’s see how I created it.

Everything starts from a UserControl class that inherits from Frame

namespace UserControl
 using Xamarin.Forms;

 public class UserControl : Frame
  private Dictionary<string, object> cache = new Dictionary<string, object>();

  public UserControl()
   this.Padding = new Thickness(0, 0, 0, 0);

  protected T FindByViewPrivate<T>(string name)
   if (this.cache.ContainsKey(name))
    return (T)this.cache[name];

   Type t = this.GetType();
   FieldInfo fi = t.GetRuntimeFields().FirstOrDefault(f => f.Name == name);
   if (fi == null) throw new NullReferenceException(string.Format("Field {0} not found.", name));
   T value = (T)fi.GetValue(this);
   this.cache.Add(name, value);
   return value;

This class just resets default padding added by Frame so that content fills the container and a FindByViewPrivate<T> method that gives us access to private UI element instances created by LoadFromXaml method when it parses the XAML.

now let’s see the “real” UserControl, XAML first:

<?xml version="1.0" encoding="utf-8" ?>
<c:UserControl xmlns=""
 <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
  <StackLayout Orientation="Horizontal">
   <Label x:Name="HeaderLabel" HorizontalOptions="Start"  />
   <Frame Padding="10,0,0,0">
    <Label TextColor="Red" x:Name="InvalidTextLabel" IsVisible="false"  />
  <Entry x:Name="TextEntry" />

then code-behind (I’ve removed Validate() method code for brevity, get the full code from the link at the end of this post)

namespace UserControl
 public enum ValidationModes

 public partial class SmartEntry : UserControl
  public SmartEntry()

   TextEntry.TextChanged += (s, e) =>
    bool isTextValid = this.Validate(e.NewTextValue);
    InvalidTextLabel.IsVisible = !isTextValid;
    if (isTextValid) this.Text = e.NewTextValue;

  public ValidationModes ValidMode { get; set; }

  public int Min { get; set; }
  public int Max { get; set; }

  public string InvalidText
   get { return InvalidTextLabel.Text; }
   set { InvalidTextLabel.Text = value; }

  public string Header
   get { return HeaderLabel.Text; }
   set { HeaderLabel.Text = value; }

  public static readonly BindableProperty TextProperty =
    BindableProperty.Create<SmartEntry, string>(
    p => p.Text, null, propertyChanged: OnTextChanged);

  private static void OnTextChanged(BindableObject bindable, string oldvalue, string newvalue)
   SmartEntry origin = (SmartEntry)bindable;
   Entry textBox = origin.FindByViewPrivate<Entry>("TextEntry");
   textBox.Text = newvalue;

  public string Text
   get { return (string)GetValue(TextProperty); }
   set { SetValue(TextProperty, value); }

I’ve added some simple public properties: Min,Max,Header,InvalidText and a BindableProperty Text so that we can bind it to our ViewModel for further processing.

As you see, inside OnTextChanged I use FindByViewPrivate<Entry> to retrieve the proper control instance from the static method.

All properties are used inside MainView’s XAML to customize each UserControl differently.

If you want to see the example in action download the code here

Have fun Winking smile

3 Responses to “UserControls in Xamarin Forms”

  1. Hi Corrado!

    I’m just getting my feet wet with Xamarin.Forms. This weekend I was able to get the CarouselPage to integrate with some scroll views. So far, so good! There seems to be real momentum behind it.

    Do you know if Xamarin.Forms apps can be deployed to other Windows 8.1 devices, like a Surface? I can’t seem to find a solid answer to that question and am very interested in doing that.

    Thanks and keep posting!

    Comment by Josh — 10/11/2014 @ 17:02

  2. Hi Josh,
    Unfortunately not yet, but Xamarin said that they’re working on it.

    Comment by corcav — 24/12/2014 @ 07:49

  3. Great job – thanks man. I really wanted to “control” a lot of my functionality.

    Comment by Dave — 27/05/2016 @ 17:17

RSS feed for comments on this post. TrackBack URL

Leave a Response