Search code examples
listviewmvvmxamarin.formscustom-renderer

Xamarin & MVVM How to?


Hi i'm new to Xamarin and I was advised to use pure MVVM because my code was a little bit of a mix between MVVM and just nothing at all.

I looked at this examples:

  1. Example1
  2. Example2

But I still can't properly define in my code what is the Model, what is the View and what is the ModelView.

Can you point me to the right structure of my projects following the MVVM?


Solution

  • Let me try to explain pure MVVM structure with reference example of Employee

    PCL :

    =>Model : which will contain all your model clases.for example employee.cs

    =>View : which will contain all xaml pages .for example employeeList.xaml etc

    =>ViewModel : which will contain all viewmodel. for example employeeViewmodel.cs in which you can defince bindable property,commands and all other mothods for operation like add,update etc. now bind this viewmodel to view using bindingContext.

    =>DependencyService : define interface which u want to implement in different platform

    Android :

    =>DependencyService : implement platform specific interface according to your requirement .

    => CustomRenderer : create custom render here for specific requirement.according to platform.

    EDIT :

    see for example you want to display total no of employees on your view(UI) that keeps changing .

    so you can create bindable property EmpCount in viewmodel that implements INotifyPropertyChanged. and now you can get set it in any method.

    now as you have already set this view model as binding context of page.its value automatically reflects on UI.