Montag, 1. April 2013

Bindings in MvvmCross V3 Touch

Stuart has done some significant improvements in V3.
This also means, that the syntax for adding the binding-description in a Touch-Project has changed to a fluent syntax:

Here we go:

this.CreateBinding(TextFieldOutlet)                
.For(textField =>   textField.Text)                
.To((MainViewModel vm) => vm.SearchText)                
.Apply();


This is the code we need in the controller to bind our TextField-Outlet to a property called "SearchText" on our ViewModel.

The "For"-part is not needed for every binding as there are several default-bindings included in the MvvmCross-Binding-Framework. For example when you bind to a TextField it uses the "Text"-property by default. Which means we could write the upper code without the "For".

Here are some more default bindings:      

registry.AddOrOverwrite(typeof (UIButton), "TouchUpInside");
registry.AddOrOverwrite(typeof (UIBarButtonItem), "Clicked");
registry.AddOrOverwrite(typeof (UITextField), "Text");
registry.AddOrOverwrite(typeof (UITextView), "Text");
registry.AddOrOverwrite(typeof (UILabel), "Text");
registry.AddOrOverwrite(typeof (MvxCollectionViewSource), "ItemsSource");
registry.AddOrOverwrite(typeof (MvxTableViewSource), "ItemsSource");
registry.AddOrOverwrite(typeof (MvxImageView), "ImageUrl");
registry.AddOrOverwrite(typeof (UIImageView), "Image");
registry.AddOrOverwrite(typeof (UIDatePicker), "Date");
registry.AddOrOverwrite(typeof (UISlider), "Value");
registry.AddOrOverwrite(typeof (UISwitch), "On");
registry.AddOrOverwrite(typeof (IMvxImageHelper), "ImageUrl");
registry.AddOrOverwrite(typeof (MvxImageViewLoader), "ImageUrl");





This for example means, that when you bind anything to a UIButton (normally you will bind an ICommand-instance). It is bound to TouchUpInside as long as you do not specify anything different using the For-Method.
This Defaults are specified in the following code (as long it is not moved XD)

2 Kommentare:

  1. Thanks for the fab posting - especially about the default properties.

    Some other tips:

    - the createbindingset mixin can help with intellisense
    - for cells and bound views, the delaybind mixin helps avoid construction order issues
    - for images there's a new mvx imageviewloader that helps with http, with local files and with resource files too

    AntwortenLöschen