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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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