Delpin Susai Raj Monday, 12 July 2021

Xamarin.Forms - Working with Behaviors in Xamarin.froms

 In this blog post, you will learn how to use Behaviors in Xamarin.Forms App


Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.


Behaviors lets you add functionality to user interface controls without having to subclass them. Behaviors are written in code and added to controls in XAML or code.

Behaviors are the best way to validate the user inputs such as email, phone number, etc.


  • Visual Studio 2017 or later (Windows or Mac)

Let's Start

Setting up a Xamarin.Forms Project

 Start by creating a new Xamarin.Forms project. You will learn more by going through the steps yourself.

Create a new or existing Xamarin forms(.Net standard) Project. With Android and iOS Platform. 

Setup UI

Now, Create a UI for your requirement. I just created a simple entry for validation purposes.


Add Behaviors

Create a new class and inherit from Behavior<T> into the class. Now I'm going to write Behavior to Entry for simple mobile number validation.

Need to implement OnAttachedTo and OnDetachingFrom from Behavior<T>


Consuming Behaviors

Now, Consume your Behavior in XAML UI Element. See below sample.

Ex: xmlns:behaviors="clr-namespace:XamarinApp"

Add Namespace

Add Attach property of Behavior in your XAML Control like below.

Full Code

You can get the full UI code below.




I hope you have understood how to use Behaviors in Xamarin.Forms App.

Thanks for reading. Please share your comments and feedback. Happy Coding :)