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 : 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.

  1. 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”.
  2. 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.

  1. 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.

  1. 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.

  1. 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.

  1. Look ma, no codebehind!

ManagingAndProgrammingMobileApplications/Xamarin/Chapter 23 (last edited 2016-11-09 23:36:55 by scot)