BindingContext Class

Definition

Helper class used to map Structures and Classes into user interface elements in MonoTouch.Dialog.

public class BindingContext : IDisposable
type BindingContext = class
    interface IDisposable
Inheritance
BindingContext
Implements

Remarks

The Reflection-based dialog construction is used by creating an object of class MonoTouch.Dialog.BindingContext, the method takes three parameters: (1) An object that will be used to resolve Tap targets, (2) The object that will be edited and (3) a title for it.

The user interface is constructed based on the type of the object being edited. The type can contain objects of the following types: string, bool, enumerations, floats, integers, DateTime, T:MonoTouch.UIKit.UIImage and those can be further decorated with a handful of attributes that drive the UI generation.

A very simple dialog that contains a checkbox is shown here:

class Settings {
    public bool AirplaneMode;
}

The above will generate a page that contains a single item with the caption "Airplane Mode" and a on/off switch. The caption is computed based on the field name. In this case "AirplaneMode" becomes "Airplane Mode". MonoTouch.Dialogs supports other conventions so "AirplaneMode", "airplaneMode" and "airplane_mode" all produce the same caption "Airplane Mode".

If you need to control the actual caption (for example to include special characters, use a different spelling or you are reusing an existing class) you just need to attach the [Caption] attribute to your variable, like this:

[Caption ("Your name is:")]
string userName;

The dialog contents are rendered in the same order that the fields are declared in the class. You can use the [Section] attribute to group information in sections that make sense. You can use the [Section] attribute in a few ways:

[Section]

This Creates a new section, with no headers or footers.

[Section (header)]

>> Creates a new section, with the specified header and no footer.

[Section (header, footer)]

>> Creates a new section with the specified header and footer.

These are the current widgets supported by the Reflection API:

String constants and Buttons.

Use the string type. If the type has a value, in addition to showing the caption, it will render its value on the right.

You can add the [OnTap] attribute to your string to invoke a method on demand.

You can add the [Multiline] attribute to your string to make the cell render in multiple lines. And you can use the [Html] attribute on a string, in that case the value of the string should contain the url to load in the embedded UIWebView.

The [Aligntment] attribute takes a parameter a UITextAlingment that determines how the string should be rendered

Examples:

public string Version = "1.2.3";

[OnTap ("Login")]
public string Login;

[Caption ("(C) FooBar, Inc")]
string copyright;

[Caption ("This is a\nmultiline caption")]
[Multiline]
string multiline;

[Caption ("Date")]
[Alignment (UITextAlignment.Center)]
string centered;

Text Entry and Password Entries.

Use the string type for your field and annotate the string with the [Entry] attribute. If you provide an argument to the [Entry] attribute, it will be used as the greyed-out placeholder value for the UITextField.

Use the [Password] attribute instead of [Entry] to create a secure entry line.

Examples:


[Entry ("Your username")]
public string Login;

[Entry]
public string StreetName;

[Password, Caption ("Password")]
public string passwd;

You can also specify both the Placeholder and the keyboard type to use on the Entry using a few of the Entry attributes:

[Entry (KeyboardType=UIKeyboardType.NumberPad,Placeholder="Your Zip code")]
public string ZipCode;

On/off switches

Use a bool value to store an on/off setting, by default you will get an On/off switch, but you can change this behavior to just show a checkbox instead by using the [Checkbox] attribute:

Examples:

bool OnOffSwitch;

[Checkbox]
bool ReadyToRun;

Float values

Using a float in your source will provide a slider on the screen. You can control the ranges of the values by using the [Range (low,high)] attribute. Otherwise the default is to edit a value between 0 and 1.

Examples:

float brightness;

[Range (0, 10), Caption ("Grade")]
float studentGrade;

Date Editing

Use a "DateTime" object in your class to present a date picker.

By default this will provide a date and time editor, if you only want to edit the date, set the [Date] attribute, if you only want to edit the time, set the [Time] attribute:

Examples:


[Date]
DateTime birthday;

[Time]
DateTime alarm;

[Caption ("Meeting Time")]
DateTime meetingTime;

Enumeration value

Monotouch.Dialogs will automatically turn an enumeration into a radio selection. Merely specify the enumeration in your file:

Examples:

enum SeatPreference { Window, Aisle, MiddleSeat }

[Caption ("Seat Preference")]
SeatPreference seat;

Images

Variables with type UIImage will render the image as a thumbnail and will invoke the image picker if tapped on.

Examples:

UIImage ProfilePicture;

Ignoring Some Fields

If you want to ignore a particular field just apply the [Skip] attribute to the field.

Examples:

[Skip] Guid UniquId;

Nested Dialogs

To create nested dialogs just use a nested class, the reflection binder will create the necessary navigation bits based on the container model.

The value for a nested dialog must not be null.

Examples:

class MainSettings {
    string Subject;
    string RoomName;
    TimeRange Time;
}

class TimeRange {
    [Time] DateTime Start;
    [Time] DateTime End;
}

To initialize:


new MainSettings () {
    Subject = "Review designs",
    RoomName = "Conference Room II",
    Time = new TimeRange {
        Start = DateTime.Now,
	End   = DateTime.Now
           }
       }

IEnumerable as a Radio Source

You can use any type that implements IEnumerable, including generics (which implement IEnumerable) as a source of values for creating a one-of-many selector, similar to the radio-like selection that you get from an enumeration.

To use this, you will need an int value that has the [RadioSelection] attribute set to hold the value that will be selected on startup and also to hold the new value when done.

For example:

       class MainSettings {
    [RadioSelection ("Themes")]
    public int CurrentTheme;
    public IList<string> Themes;
}

The value rendered is the value rendered by calling ToString() on the value returned by IEnumerable.

Creating a Dialog From the Object

Once you have created your class with the proper attributes, you create a binding context, like this:

BindingContext context;

public void Setup ()
{
    // Create the binder.
    context = new BindingContext (this, data, "Settings");

    // Create our UI
    // Pass our UI (context.Root) and request animation (true)
    var viewController = new DialogViewController (context.Root, true);

    navigation.PushViewController (viewController, true);
}

This will render the information. To fetch the values back after editing you need to call context.Fetch (). You can do this from your favorite handler, and at that point you can also call context.Dispose() to assist the GC in releasing any large resources it might have held.

Constructors

BindingContext(Object, Object, String)

Creates a binding context with the specified parameters.

Fields

Root

The produced RootElement that you can pass to a DialogViewController.

Methods

Dispose()
Dispose(Boolean)
Fetch()

Retrieves the data that was edited by the user and stores it back into the original object that was passed to the BindingContext.

Applies to