Behaviors

Behaviors are a special technique that lets us add functionality to your UI control without having to subclass the control. Behaviors are implemented to code and attached to the controls written in XAML or code. Behaviors interact directly with the APIs exposed by the controls and hence they can be packaged with the control and reused across various applications. Behaviors allow adding new functionality like e.g. an Email Validator to an Entry that accepts Email as user input. Another use case could be a spelling checker with the Entry that helps you correct your spelling while typing. Other typical uses of behaviors are to add an effect to control or to control the control animation.

Behaviors are usually of following types:

  • Xamarin.Forms behaviors: classes that derive from the Behavior or Behavior<T> class where T is the type of control to which the behavior should apply

  • Attached behaviors: These are static classes with one or more attached properties

  • Reusable Behaviors: Behaviors that are reusable across more than one application.

Behaviors are written for a specific control type ), and hence, they are supposed to be added only to a compatible control. Attempting to attach a behavior to an incompatible control will result in an exception being thrown.

Last updated