Member-only story

Behaviors -.Net MAUI

Swetha vennapusa
3 min readFeb 24, 2023

.Net MAUI Behaviors allow you to enhance the functionality of a control without subclassing. Instead, the functionality is implemented in a behavior class and attached to the control as if it was part of the control itself.

.Net MAUI Behaviors are similar to Xamarin.Forms Behaviors. Behaviors enable you to code reusability across applications and help you to avoid writing code in code behind instead we can maintain code in a behavior class. They can be used to provide full functionality to controls like

Event to command behaviors

Entry Validators

Handling events raised in a control etc

There are 2 types of behaviors

  • Attached behaviors
  • .NET MAUI behaviors

Attached behaviors

Attached behaviors are static classes with one or more attached properties. An attached property is a special type of bindable property or a custom property. They are recognizable in XAML as attributes.

Let’s create an attached behavior “MaxLengthValidatorBehavior” for entry. It changes the text color to red if the characters entered exceeds max length given

     public static class MaxLengthValidatorBehavior
{
public static readonly BindableProperty AttachBehaviorProperty =
BindableProperty.CreateAttached("AttachBehavior", typeof(bool),
typeof(MaxLengthValidatorBehavior), false…

--

--

No responses yet