Chapter 23 Notes

Title: Triggers and behaviors

Summary

Relates to XAML allowing triggers and behaviors.

Programming Concepts Summary

Concept

Page

Programs

Program EntryPop

Page 837.

Concepts:

  1. Shows an Entry changed by a trigger. .

Program EntryPopCode

Page 839.

  1. Shows previous program but set in code. Not interesting really since the whole purpose was to do it in XAML.


However there is no support for animations in XAML, this requires code! See new class: Easing on page 432.


Program EntrySwell

Page 844.

  1. Shows the implementation in XAML of a ScaleAction.

New Classes:

  1. ScaleAction

EnterExitSwell

Page 846.

  1. Demo's both directions...

New Classes:

  1. Trigger.EnterActions

  2. Trigger.ExitActions

Program ButtonGrowth

Page 849

  1. Demo's making buttons grow and shrink.

Program SiverButtonDemo

Page 851.

  1. Like it says make it shiver ... its more about the toolkit!


Don't have a tapped event? Use a behavior p. 851... Later!


Program TriggerEntryValidation

Page 852.


Data Trigers


Program GenderColors

Page 854.

  1. Sets text blue or pink on the color base on gender. (See p. 855)

Program ButtonEnabler

Page 856.

  1. Sets the button enabled based on value of an Entry view.

Program AndConditions

Page 859.

  1. Hmm. Multi-Triggers default to an "AND" condition. Mildly interesting. Value=True...

Program OrConditions

Page 861.

  1. Ahh, you can set it to be an OR condition too. Even less Mildly interesting. Value=False...

Program XorConditions

Page 866.

  1. Yeeaasss! We can do XOR too.


Behaviors Anything you can do with a Trigger you can also do with a Behavior. However, a behavior always involves some code, which is a class that derives from Behavior<T>. Triggers only involve code if you’re writing an Action<T> derivative for an EventTrigger or for EnterActions or ExitActions collections of the other triggers.


Program TriggerEntryValidation

Finally this is important!

Page 868.

New Classes:

  1. NumericValidationBehavior (869)

Program EmailValidationDemo

Page 872.

  1. Demo this one it has Regular Expressions in it!

Program EmailValidationTrigger

Page 876.

  1. Demos the use of a trigger with EmailValidation


Toggles and Check boxes


Program ToggleLabel

Page 877.

  1. Demos the ability to use a button like a toggle.

Program FormattedTextTogle

Page 878.

Expands previous program: Of course, such a program would probably need to run some code when the Label is toggled. Keep in mind that Behavior derives from BindableObject, which means that any BindableProperty that you define in a behavior automatically generates a PropertyChanged event when the property changes.

This means that you can attach a handler to the PropertyChanged event of ToggleBehavior and check for changes in the IsToggled property. This is demonstrated in the FormattedTextToggle program.

Program TraditionalCheckBox

Page 881.

  1. uses two Unicode characters for an unchecked box and a checked box, similar to the CheckBox views in Chapters 15 and 16.

Program SwitchCloneDemo

Page 884.

  1. It’s entirely done in XAML!
  2. Yuk, the XAML one didn't work, so they included a Code only version .... SKIP IT!

Program LeverToggle

Page 888.

  1. Fun with BoxView elements... skippable.


Responding to Taps: The various manifestations of toggle views demonstrate one way to respond to taps within a XAML file. If tap events were integrated into the VisualElement class, you could get at them more directly and with greater ease using EventTrigger. But you can’t attach an EventTrigger to a TapGestureRec-ognizer. Getting around that little restriction is the purpose of a behavior devoted solely to a tap. This is called TapBehavior (889)


Program BoxViewTapShiver

Page 891.

  1. Attaches TapBehavior objects to three Box-View elements, each of which also includes a DataTrigger that references the behavior and invokes a ShiverAction in its EnterActions collection.

Program ShiverViews

Page 892.

  1. If you want to cut down on the markup in this case, you’ll once again need to define a new class. The ShiverViews program demonstrates this.


Radio Buttons Page 893