Triggers in Xamarin Forms

In Mobile Development by Christian HissibiniLeave a Comment

Triggers take a trigger source such as an event and then can change a property on that control in response to the trigger. The basic principle is, a trigger is activated and you set properties or invoke code. Below are the following triggers you can use.

  • Property
  • Data
  • Event
  • Multi Trigger

Documentation: Working with Triggers

Example Use Case: Update the Background Color of Entry control when IsFocused becomes true.

public class BackgroundColorTrigger : TriggerAction<Entry> {          protected override void Invoke(Entry sender)      {          sender.BackgroundColor = Color.Yellow;      } }

Then we can apply it to an Entry Control and define what triggers the trigger.

// Add to Page Attributes (Above Trigger is in Namespace Mobile.Trigger) xmlns:trigger="clr-namespace:Mobile.Trigger" <Entry Text="{Binding EntryField}">        <Entry.Triggers>         <EventTrigger Event="Focused">             <trigger:BackgroundColorTrigger />         </EventTrigger>     </Entry.Triggers> </Entry>

Ref – –

Leave a Comment