SimpleViewModel - markjulmar/xamu-infrastructure GitHub Wiki
SimpleViewModel
The SimpleViewModel class provides a simple base class for the Model-View-ViewModel design pattern. It implements the necessary property change notification support used by the Xamarin.Forms data binding infrastructure. You can use this class as your base class for any view models you create in your app.
Methods
RaiseAllPropertiesChanged: tells any attached bindings that all properties on this instance must be re-evaluated.RaisePropertyChanged: raises a single property change notification. Has variations for type-safe invocation and string-based invocation.SetPropertyValue: changes a field value to a new passed value and raises a property change notification if the field was altered. Returnstrueif the field was changed.
Note: All the methods are protected and intended to be used only by derived classes.
Example
public class MyViewModel : SimpleViewModel
{
public bool HasText
{
get { return text.Length > 0; }
}
private string text = "";
public string Text {
get { return text; }
set {
if (SetPropertyValue(ref text, value)) {
RaisePropertyChanged(nameof(HasText));
// or this if you aren't using C# 6
RaisePropertyChanged(() => HasText);
}
}
}
}