forked from xamarinhq/xamu-infrastructure
-
Notifications
You must be signed in to change notification settings - Fork 0
SimpleViewModel
Mark Smith edited this page Aug 29, 2016
·
1 revision
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.
-
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. Returnstrue
if the field was changed.
Note: All the methods are protected and intended to be used only by derived classes.
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);
}
}
}
}