Xamarin.Forms Projects
上QQ阅读APP看书,第一时间看更新

Using MVVM – creating Views and ViewModels

MVVM is all about the separation of concerns. Each part has a specific meaning:

  • Model: This relates to anything that represents data and that can be referenced by the ViewModel
  • View: This is the visual component. In Xamarin.Forms, this is represented by a page
  • ViewModel: This is a class that acts as the glue between the Model and the View

In our app, we could say that the Model is the repository and the to-do list items it returns. The ViewModel has a reference to this repository and exposes properties that the View can bind to. The ground rule is that any logic should reside in the ViewModel and no logic should be in the View. The View should know how to present data, such as converting a Boolean value to Yes or No.

MVVM can be implemented in many ways and there are quite a few frameworks that we could use. In this chapter, we have chosen to keep things simple and implement MVVM in a vanilla way, without any framework at all.