Triggers in Xamarin.Forms 1.3

By December 12, 2014 News, Xamarin 2 Comments
Trigger at work

Intro

Yesterday, I wrote about one of the new features in Xamarin.Forms 1.3 (Technology Preview 2), Styles.  Today I’ll take a first look at Triggers.

Triggers

The key idea of Triggers is “When this happens, do that.” More specifically, “When this property has that value, then set this other property to this new value.”  Huh?  Perhaps this is easier to show than to explain.

Let’s suppose we’re building a form and we’d like to enhance the Entry control so that when the user is entering data the text color is blue, but when they are done the text color turns black.

Step one is to create a style in the Resource Dictionary and to give that Style a set of Triggers,

 

Inside the Triggers element you’ll place one or more Trigger.  Each trigger will identify the property and value combination that will fire the trigger and then what action to take. The action is designated by a setter, which again takes a property and a value (the value to assign to the property when triggered)

That’s it.  When the user gives focus to an Entry in your form, the color of the text in that entry will be Red, as shown in the figure, below.  For context, here is the entire XAML fiile:

 

Triggers at work

Triggers at work

 
 
 
 
 
 
 
 

Event Triggers

Triggers can do a lot more, of course.  One of the very cool things you can do with triggers is to user a TriggerAction to modify a property from code.  For example, let’s say we want to turn the text red if the user’s entry is not valid, e.g., we’re looking for an integer and they enter a letter or they try to enter a decimal amount.  We could do this in code, but it is cleaner to do this in markup.  For this to work, we’ll define a class that derives from TriggerAction and designate that it is targeted at Entry views,

With this in place, we return to our XAML and declare a local namespace in the ContentPage element (in this case, the project name is Triggers1),

We can now find our Entry and add the validation to it.  We don’t need to validate Name (not an integer) but we do want to validate Age,

That is the only change we need to make to the XAML.  There is no change in the Resource Dictionary. The result is demonstrated in this video, which shows that if you try to enter a letter or a decimal point in the Age view the entry turns red.

 

 
 
There is a good deal more to say about Triggers but they are in flux during the Technical Preview, so the next posting will move on to the third cool feature in 1.3: behaviors.

 

 

 

The following two tabs change content below.

Jesse Liberty

Master Consultant at Falafel Software
Jesse Liberty is a Master Consultant for Falafel Software, an author and he creates courses for Pluralsight . He is a Microsoft MVP and a Certified Xamarin Mobile Developer. Liberty hosts the popular Yet Another Podcast and his blog is considered required reading. He was a Senior Evangelist for Microsoft, a XAML Evangelist for Telerik, Distinguished Software Engineer at AT&T; Software Architect for PBS and Vice President of Information Technology at Citibank.

Latest posts by Jesse Liberty (see all)

  • Pingback: Dew Drop – December 15, 2014 (#1915) | Morning Dew()

  • Dale C Stewart

    we’ll define a class that derives from TriggerAction and designate that it is targeted at Entry views, The type or namespace name “TriggerAction could not be found. Are you missing an assembly reference? Do you have a solution for this. Thanks