public class PropertyChangedEventArgs : EventArgs { public virtual string PropertyName { get; } } public interface INotifyPropertyChanged { event PropertyChangedEventHandler PropertyChanged; }
public class Monkey : IHasLongId, IReturn<Monkey> { public long Id { get; set; } public bool IsPrimate { get; set; } public string Text { get; set; } }
public class MonkeyRequest : IReturn<Monkey> { … } public class MonkeyViewModel : ServiceClientViewModelBase { public Monkey Monkey { get; set; } public void OnInitialize(long id) { var req = new MonkeyRequest(id); RequestAndSetProperty(req, () => Monkey); } }
Monkey Monkey { get; set; } string Text { get; set; } bool IsTextEnabled { get; set; } void SaveText() { if (Monkey.Id == 0) { Monkey.Text = Text; PostAndSetProperty(Monkey, () => Monkey, "IsTextEnabled"); } else // post update … }
public class MonkeyTextUpdate : IReturn<Monkey> { public long Id { get; set; } public string Text { get; set; } } // … else /* Monkey.Id != 0 */ { var req = new MonkeyTextUpdate(Monkey.Id, Text); PostAndSetProperty(req, () => Monkey, "IsTextEnabled"); }
public class MonkeyListRequest : IReturn<IList<Monkey>> { ... } public class MonkeyListViewModel : ServiceClientViewModelBase { public IList<Monkey> Monkeys { get; set; } void RequestMonkeyList() { var req = new MonkeyListRequest(); RequestAndSetProperty(req, () => Monkeys); } }
IList<Monkey> Monkeys { get; set; } public void AddMonkey() { var newMonkey = new Monkey { … }; PostAndAdd(newMonkey, () => Monkeys); } public void RemoveMonkey(Monkey monkey) { DeleteAndRemove(monkey, () => Monkeys); }
IList<Monkey> Monkeys { get; set; } string NewMonkeyText { get { … }; set { … Notify(() => NewMonkeyText); } bool CanAddMonkey { get { … }; set { … Notify(() => CanAddMonkey); } public void AddMonkey() { CanAddMonkey = false; var newMonkey = new Monkey { Text = NewMonkeyText }; PostAndAdd(newMonkey, () => Monkeys, () => { // On Success NewMonkeyText = null; CanAddMonkey = true; }, // On Error () => CanAddMonkey = true); }
Monkey Monkey { get; set; } bool IsPrimate { get { return Monkey.IsPrimate; } set { Monkey.IsPrimate = value; } } bool IsPrimateIsEnabled { get; set; } public void ToggleIsPrimate() { bool newPrimate = !IsPrimate; // If the update fails, we haven't changed // the local copy misleadingly var req = new Monkey(Monkey) { IsPrimate = newPrimate }; PostUpdateForProperty(req, () => IsPrimate, newPrimate); }
Preserve Attribute on DTO & VM assemblies
#if MONOTOUCH [assembly: MonoTouch.Foundation.Preserve(AllMembers = true)] #endif
Update on Main/UI Thread
static void HandlePropertyChangedFromEventThread( this ICanHandlePropertyChanges source, object sender, PropertyChangedEventArgs e) { source.InvokeOnMainThread(() => { if (source.IsViewLoaded) source.HandlePropertyChanged(e.PropertyName); }); }