Chapter 2. MVVM and Data Binding

In this chapter, we will look at the Model-View-ViewModel (MVVM) pattern and the MVVM elements that come with the Xamarin.Forms toolkit, and how we can expand on them to truly take advantage of the power of the pattern. As we dig into these topics, we will apply them to the TripLog app that we started building in Chapter 1, Getting Started.

In this chapter, we will cover the following:

  • Understanding the MVVM pattern and data binding
  • MVVM in the Xamarin.Forms toolkit
  • Adding the MVVM pattern and data binding to the Xamarin.Forms mobile app created in Chapter 1, Getting Started

Understanding the MVVM pattern

At its core, MVVM is a presentation pattern designed to control the separation between user interfaces and the rest of the application. The key elements of the MVVM pattern are as follows:

  • Models: Models represent the business entities of an application. When responses come back from an API, they are typically deserialized to models.
  • Views: Views represent the actual pages or screens of an application, along with all of the elements that make them up, including custom controls. Views are very platform-specific and depend heavily on platform APIs to render the application's user interface (UI).
  • ViewModels: ViewModels control and manipulate the Views by serving as their data context. ViewModels are made up of a series of properties represented by models. These properties are part of what is bound to the Views to provide the data that is displayed to users, or to collect the data that is entered or selected by users. In addition to model-backed properties, ViewModels can also contain commands, which are action-backed properties that bind actual functionality and execution to events that occur in the Views, such as button taps or list item selections.
  • Data binding: Data binding is the concept of connecting data properties and actions in a ViewModel with the user interface elements in a View. The actual implementation of how data binding happens can vary and in most cases, is provided by a framework, or API. In Windows app development, data binding is provided declaratively in XAML. In traditional (non-Xamarin.Forms) Xamarin app development, data binding is either a manual process or dependent on a framework such as MvvmCross (link to the Mvvmcross site / GitHub page, https://github.com/MvvmCross/MvvmCross), a popular framework in the .NET mobile development community. Data binding in Xamarin.Forms follows a very similar approach to Windows app development.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.176.145