A set of helper classes for the basic MVVM app.
TrakPropertyChangedBase implements the INotifyPropertyChanged interface to save coding.
TrakViewModelBase inherits TrakPropertyChangedBase and has 2 properties, VMList and EditVMRecord.
TrakPropertyChangedBase
Implements the INotifyPropertyChanged interface.
Method:
RaisePropertyChanged(); – Add this method to all properties when you set the value for the property.
Example:
using ComponentTrak.Helpers;
class MyViewModel : TrakPropertyChangedBase
{
private string _Name = “”;
public string Name { get { return _Name; } set { _Name = value; RaisePropertyChanged(); } }
}
TrakViewModelClassBase
Implements the INotifyPropertyChanged interface and has properties for the basic MVVM using C# generics.
Method:
RaisePropertyChanged(); – Add this method to all properties when you set the value for the property.
Properties:
VMList – Declares an observable collection using T.
EditVMRecord – Declares an edit record.
Example:
Always add the using statement at the top of your cs file.
using ComponentTrak.Helpers;
This class also implements the TrakPRopertyChangedBase class so you can use the RaisePropertyChanged on any of your custom properties.
class MyViewModel : TrakViewModelClassBase
{
private string _Name = “”;
public string Name { get { return _Name; } set { _Name = value; RaisePropertyChanged(); } }
}
In your MyContentPage.xaml to use the built-in list of models, you could bind it to your ListView
<ListView ItemsSource=”{Binding VMList}”/>
In your MyContentPage.xaml to edit a record using the built-in model, you could bind your grid or any control to the EditVMRecord that has a model with multiple properties. This example shows if the model was a customer model with a couple of properties, Name and Address
<Grid BindingContext=”{Binding EDitVMRecord}”>
<Entry Text=”{Binding Path=Name, Mode=TwoWay}”/>
<Entry Text=”{Binding Path=Address, Mode=TwoWay}”/>
</Grid>
TrakViewModelValidationBase-
Implements the INotifyPropertyChanged interface and has properties for the basic MVVM using C# generics.
Method:
RaisePropertyChanged(); – Add this method to all properties when you set the value for the property.
Properties:
VMList – Declares an observable collection using T.
EditVMRecord – Declares an edit record.
Example:
Always add the using statement at the top of your cs file.
using ComponentTrak.Helpers;
This class also implements the TrakPRopertyChangedBase class so you can use the RaisePropertyChanged on any of your custom properties.
class MyViewModel : TrakViewModelClassBase
{
private string _Name = “”;
public string Name { get { return _Name; } set { _Name = value; RaisePropertyChanged(); } }
}
In your MyContentPage.xaml to use the built-in list of models, you could bind it to your ListView
<ListView ItemsSource=”{Binding VMList}”/>
In your MyContentPage.xaml to edit a record using the built-in model, you could bind your grid or any control to the EditVMRecord that has a model with multiple properties. This example shows if the model was a customer model with a couple of properties, Name and Address
<Grid BindingContext=”{Binding EDitVMRecord}”>
<Entry Text=”{Binding Path=Name, Mode=TwoWay}”/>
<Entry Text=”{Binding Path=Address, Mode=TwoWay}”/>
<Button Text=”Save” IsEnabled=”{Binding Path=IsModelValid}”/>
</Grid>
In this part of the example, we’ll add validation name to be required and string length.
var vm = BindingContext as TrakViewModelValidationBase<Customer>;
vm.AddTrakValidator(new TrakValidator(“Name”,
new TrakValidationRequired(“Name is required”),
new TrakValidationStringLength(3, 100, “Name must be 3 to 100 characters”)));
vm.AddTrakValidator(new TrakValidator(“Address”, new TrakValidationStringLength(4, 100, “Address must be 4 to 100 characters”)));