View Model Items on ListView Xamarin Forms

Hey today I'll show you how to implement the MVVM pattern to update, add and remove data in a listview on Xamarin Forms.

Here the root project files



  • User.cs : This file contains our user model data in this case Name and Age.
  • UserVM.cs : This file contains all the logic to bind data between view and root model also implements INotifyPropertyChanged pattern.
  • EditUserPage.cs : This is the page to edit user info have two entries and on button to save data.
  • MainPage.cs : This page contains the main listview and a button to add a new user to the listview data we use an ObservableCollection that allow auto-refresh listview data when you modify the datasource, also I implement the listview ContextActions to allow user to modify the listview items with two options edit and delete(iOS swipe left and Android/WP Long click on cell).
Now we have the code for each file:


Here the result:





If you want to have access to full example go to this link https://github.com/AlejandroRuiz/XamarinFormsViewModel happy code also if you have doubts plea let me know via comments

Comentarios

Entradas populares