Chapter 11 Notes

Title: The Bindable Infrastructure

Summary

This will be our introduction to binding views to properties, i.e. Data Binding.

Programming Concepts Summary

Concept

Page

Property

227

Xamarin enhances property definition and encapsulates this enhanced property in the BindableProperty class.

227

BindableProperty objects are used to store the data associated with a BindableObject

22 + Docs online

In general there are two extremely important concepts in CS: abstraction and layering.

Properties are an abstraction of implementing getters and setters for a field. The abstraction allows the the user to treat the property like a field (variable), and as such we are abstracting away the details of getters and setters so that the user of a property does not have to remember to use a getter and setter when dealing with a property vs. a normal field variable.

The class hierarchy project shows the process of layering functionality. Functionality shared by every object is at the top of the inheritance tree, and every object inherits from that object directly or through an inheritance chain. This is demonstrated nicely in the ClassHierarchy program.

Programs

Program ClassHierarchy

Page 228.

Concepts:

  1. Shows how to walk the Xamarin.Forms class hierarchy so that we can see how the classes are related.
  2. General functionality is defined and implemented higher in the hierarchy this includes all functionality (and implementation code) shared by the children. Although this may seem obvious to say, I want to emphasize that this is a layering technique similar to what you learned in CPTR 328 (Principles of Networking). I.e. different functionality is defined at different layers in the inheritance hierarchy.

Classes:

  1. ClassesAndSubclasses is a data class used to build the hierarchy.

  2. TypeInformation adds some functionality needed to interrogate types.

  3. ClassHierarchyPage : ContentPage contains most of the functionality. (Opitonal)

Program PropertySettings

Page 235.

Concepts:

  1. Exposes the underlying BindableProperty by calling the label1.SetValue(Label.TextProperty, "Some Text"); function (defined in BindableObject instead of using the common shortcut method of label1.Text="Some Text");

    1. Label.TextProperty is a BindableProperty defined in the Label class as:

   1 public static readonly BindableProperty TextProperty;
   2 
   3 public string Text
   4 {
   5    set { SetValue(Label.TextProperty, value); }
   6    get { return (string)GetValue(Label.TextProperty); }
   7 }

Classes: No new classes.

Program DynamicVsStaticCode

Page 238.

Concepts:

  1. This program shows the motivation for BindableProperty classes.

  2. For the reasons given, whenever you create a custom view and need to define public properties, your default inclination should be to define them as BindableProperty objects!

Classes: No new classes.

AltLabel

Page 241.

Concepts:

  1. Extends Label with a BindableProperty named PointSize to allow you to set the font size with a point value.

  2. Shows how to create a BindableProperty see Page 242.1.

Classes:

  1. AltLabel

PointSizedText

Page 245.

Concepts:

  1. Demonstrates the use of AltLabel

Classes: No new classes.

CountedLabel

Page 246

Concepts:

  1. Demos how to create a readonly property, namely wordcount, which of course should not be set!
  2. Re-introduces INPC INotifyPropertyChanged interface and event.

BaskervillesCount

Page 249.

Demonstrates the CountedLabel class.

Classes: CountedLabel

ManagingAndProgrammingMobileApplications/Xamarin/Chapter 11 (last edited 2016-09-25 19:09:09 by scot)