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:
- Shows an Entry changed by a trigger. .
Program EntryPopCode
Page 839.
- 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.
Shows the implementation in XAML of a ScaleAction.
New Classes:
EnterExitSwell
Page 846.
- Demo's both directions...
New Classes:
Trigger.EnterActions
Trigger.ExitActions
Program ButtonGrowth
Page 849
- Demo's making buttons grow and shrink.
Program SiverButtonDemo
Page 851.
- 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.
- Sets text blue or pink on the color base on gender. (See p. 855)
Program ButtonEnabler
Page 856.
- Sets the button enabled based on value of an Entry view.
Program AndConditions
Page 859.
- Hmm. Multi-Triggers default to an "AND" condition. Mildly interesting. Value=True...
Program OrConditions
Page 861.
- Ahh, you can set it to be an OR condition too. Even less Mildly interesting. Value=False...
Program XorConditions
Page 866.
- 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:
Program EmailValidationDemo
Page 872.
- Demo this one it has Regular Expressions in it!
Program EmailValidationTrigger
Page 876.
Demos the use of a trigger with EmailValidation
Toggles and Check boxes
Program ToggleLabel
Page 877.
- 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.
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.
- It’s entirely done in XAML!
- Yuk, the XAML one didn't work, so they included a Code only version .... SKIP IT!
Program LeverToggle
Page 888.
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.
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.
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 : The radios built into the dashboards of old automobiles often featured a row of half a dozen (or so) buttons that could be “programmed” for various radio stations. Pushing in one of these buttons caused the radio to jump to that preselected station, and also caused the button for the previous selection to pop out. Those old car radios are now antiques, but mutually exclusive options on our computer screens are still represented by visual objects we call radio buttons.
Traditionally, radio buttons were grouped with a common parent. In Xamarin.Forms terminology, radio buttons that are children of one StackLayout are considered to be in the same group, while radio buttons that are children of another StackLayout are in another independent group.
See the RadioBehavior class in the toolkit.
Program RadioLabels
Page 897.
- Here’s a simple demonstration of some interactive logic for selecting the size of a T-shirt. The three radio buttons are simple Label elements with text properties of “Small”, “Medium”, and “Large”.
The RadioLabels program avoids those issues and simply binds the IsChecked properties of the three RadioBehavior objects to the IsVisible properties of three Image elements sharing a single-cell Grid at the bottom of the XAML file.
Program RadioStyle
Page 900.
Demonstrates a better approach which consolidates the Setter objects in a Style, and then to reference the Style in the DataTrigger.
Radio Images
Page 902.
The RadioImages program contains four bitmaps indicating different modes of transpor-tation. The Image elements that reference these bitmaps are each a child of a ContentView to which is attached the RadioBehavior and a DataTrigger that changes the color of the ContentView.
TraditionalRadios
Page 905.
The TraditionalRadios program has six radio buttons, but they are divided into two groups of three buttons each, so the GroupName properties need to be set for at least one of the two groups. The program chooses to set the GroupName for all the radio buttons to either “platformGroup” or “lan-guageGroup”. Each RadioBehavior is attached to a horizontal StackLayout that contains one Label with a DataTrigger that switches between the “○” and “◉” strings, and a sec-ond Label that displays the text to the right of that symbol.
Fades and Orientation (908).
MultiColorSliders
You should read over the preamble for this - it is quite extensive!
Page 913.
- Look ma, no codebehind!