Rio Binding today is all about ViewModels.
Rio is an attempt to experiment with different types of ViewModel. In particular to:
- use
- fields
- methods
- instead of
- properties
- commands.
Using the FieldBinding and MethodBinding plugins from Rio, a view model can look like:
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 FirstViewModel | |
: MvxViewModel | |
{ | |
public readonly INC<string> FirstName = new NC<string>(""); | |
public readonly INC<string> LastName = new NC<string>(""); | |
public readonly INC<TitleResponse> Title = new NC<TitleResponse>(); | |
public readonly INC<bool> Accepted = new NC<bool>(); | |
public readonly ObservableCollection<Person> People = new ObservableCollection<Person>(); | |
public readonly List<TitleResponse> Titles = new List<TitleResponse>() | |
{ | |
TitleResponse.None, | |
TitleResponse.Mr, | |
TitleResponse.Dr, | |
TitleResponse.Mrs, | |
TitleResponse.Ms, | |
TitleResponse.Miss, | |
}; | |
public void Add() | |
{ | |
if (!Accepted.Value) | |
return; | |
People.Add(new Person() | |
{ | |
FirstName = FirstName.Value, | |
LastName = LastName.Value, | |
Title = Title.Value | |
}); | |
Title.Value = TitleResponse.None; | |
FirstName.Value = ""; | |
LastName.Value = ""; | |
Accepted.Value = false; | |
} | |
} |
instead of an old-skool-INotifyPropertyChanged ViewModel like:
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 FirstViewModel | |
: MvxViewModel | |
{ | |
private string _firstName; | |
public string FirstName | |
{ | |
get { return _firstName; } | |
set { _firstName = value; RaisePropertyChanged(() => FirstName); } | |
} | |
private string _lastName; | |
public string LastName | |
{ | |
get { return _lastName; } | |
set { _lastName = value; RaisePropertyChanged(() => LastName); } | |
} | |
private List<TitleResponse> _titles = new List<TitleResponse>() | |
{ | |
TitleResponse.None, | |
TitleResponse.Mr, | |
TitleResponse.Dr, | |
TitleResponse.Mrs, | |
TitleResponse.Ms, | |
TitleResponse.Miss, | |
}; | |
public List<TitleResponse> Titles | |
{ | |
get | |
{ | |
return _titles; | |
} | |
} | |
private TitleResponse _title; | |
public TitleResponse Title | |
{ | |
get { return _title; } | |
set { _title = value; RaisePropertyChanged(() => Title); } | |
} | |
private bool _accepted; | |
public bool Accepted | |
{ | |
get { return _accepted; } | |
set { _accepted = value; RaisePropertyChanged(() => Accepted); } | |
} | |
public ICommand AddCommand | |
{ | |
get | |
{ | |
return new MvxCommand(() => | |
{ | |
if (!Accepted) | |
return; | |
People.Add(new Person() | |
{ | |
FirstName = FirstName, | |
LastName = LastName, | |
Title = Title | |
}); | |
Title = TitleResponse.None; | |
FirstName = ""; | |
LastName = ""; | |
Accepted = false; | |
}); | |
} | |
} | |
private ObservableCollection<Person> _people = new ObservableCollection<Person>(); | |
public ObservableCollection<Person> People | |
{ | |
get { return _people; } | |
set { _people = value; RaisePropertyChanged(() => People); } | |
} | |
} |
Rio is an attempt to start a conversation.:
- Which ViewModel style do you prefer?
- Why?
- What else could be tried?
- How could anyone do better?
Please note that no-one is saying that Rio field- and method-binding is better than the established property and command binding. All it does for now is to offer some new alternatives. Always think about your code and always choose the tools and techniques which suit you, your team and your app best - the app is king!
The source for today's video is at: https://github.com/slodge/NPlus1DaysOfMvvmCross/tree/master/N-36-Rio
A full N+1 index is available on:
No comments:
Post a Comment