2

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?

Neelam Prajapati
  • 3,764
  • 3
  • 28
  • 62
Tiago_nes
  • 843
  • 7
  • 30
  • It looks like you are missing the ViewModels. You have a View (that view holding your ListView) and a Model (StoreModel) but you are just missing a ViewModel that "connects" them. Just create a ViewModel and let your controls. (ListView) bind it's properties and events onto that model and your done – Tobias Theel Dec 20 '17 at 11:11
  • @Tobias Theel That would be very simple if i hadn't the CustomRenderers which must deal with the bindings themselves, and in case of Android has its own CellView. – Tiago_nes Dec 20 '17 at 11:15
  • I don't get why that prevents you from binding the view properties onto a viewModel. Could you please explain that part a little more detailed? – Tobias Theel Dec 20 '17 at 11:17
  • 3
    There are a lot of articles about MVVM and implementing MVVM with Xamarin Forms, start from understanding the MVVM it self. Check this out https://developer.xamarin.com/guides/xamarin-forms/enterprise-application-patterns/mvvm/ – EvZ Dec 20 '17 at 11:30
  • @Tobias Theel I tried to explain a little bit more... is it enough? – Tiago_nes Dec 20 '17 at 11:37

1 Answers1

4

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.

Neelam Prajapati
  • 3,764
  • 3
  • 28
  • 62
  • 1
    What about the `ContentPage.BindingContext` and the `{Binding Property}` and `BindableProperty` I cant really understand how they are used... – Tiago_nes Dec 20 '17 at 14:27
  • @Tiago_nes please check my EDIT. and let me know if still you have any confusion – Neelam Prajapati Dec 21 '17 at 05:28
  • 1
    Same about `ObservableCollection`? I dont need to Implement `INptifyPropertychanged` when i use the collection rigth? – Tiago_nes Dec 21 '17 at 16:39
  • it should be actually.but sometimes it dont reflect.so i will suggest you to implement INotifyPropertyChanged and create bindable property for it.for better practice. – Neelam Prajapati Dec 22 '17 at 05:35