Wednesday, September 25, 2013

N=39 - CrossLight on Xamarin.iOS/MonoTouch (aka Project Chimp)

This video is a follow on from http://slodge.blogspot.co.uk/2013/06/n30-crosslight-aka-project-chimp-n1.html where I showed how to use the MvvmCross binding layer in Android without using the higher layers (the navigation service, the plugin loader, the thread dispatcher, etc).


In this video I quickly do the same thing for Xamarin.iOS.


The process is pretty simple:


1. Add references to the CrossCore and Binding assemblies for MvvmCross (in the video I use nuget CrossCore package - https://www.nuget.org/packages/MvvmCross.HotTuna.CrossCore/)

2. Add a small setup class and call it from your appdelegate start code


public class MiniSetup
{
public static readonly MiniSetup Instance = new MiniSetup();
public void EnsureInit()
{
if (MvxSimpleIoCContainer.Instance != null)
return;
var ioc = MvxSimpleIoCContainer.Initialise();
var builder = new MvxTouchBindingBuilder();
builder.DoRegistration();
}
}
view raw gistfile1.cs hosted with ❤ by GitHub

3. Add you ViewModel - and INotifyPropertyChanged will do

4. Adjust your ViewController so that it can do data-binding to an instance of your ViewModel


public class MyViewController
: UIViewController
, IMvxBindable
{
public IMvxBindingContext BindingContext { get; set; }
protected override void Dispose(bool disposing)
{
if (disposing)
{
BindingContext.ClearAllBindings();
}
base.Dispose(disposing);
}
public object DataContext
{
get { return BindingContext.DataContext; }
set { BindingContext.DataContext = value; }
}
public MyViewController()
{
this.CreateBindingContext();
DataContext = new MyViewModel();
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// create your UIViews
var set = this.CreateBindingSet<MyViewController, MyViewModel>();
// bind your UIViews
set.Apply();
}
}
}

5. Run :)


The code for today is at: https://github.com/slodge/NPlus1DaysOfMvvmCross/tree/master/N-39-CrossLight-Touch


The video is:




For a full index of N+1 videos, see http://mvvmcross.wordpress.com

No comments:

Post a Comment