Introduction

The Model-View-ViewModel (MVVM) pattern is a software design pattern. Design patterns are sets of rules and templates that help make application code better or more consistent. You use the MVVM pattern to help separate the app's behavior logic from the user interface-rendering code, and to do so in a way that supports unit testing.

Say your company produces management software for human resources departments. They use the software to manage and review details about the employees of the company. The existing system is web-based. You were asked to create a mobile app that can be used with the same back-end APIs that the web system uses. You know that the back-end APIs and the web front end are thoroughly covered by unit tests. And you want to maintain that standard for the mobile app. You heard that the MVVM pattern can help design mobile apps for maximum testability, so you plan to try it out.

MVVM itself is a design pattern that's the basis for many frameworks and programming tool-kits. Most of those frameworks also provide other things, like navigation and messaging abstractions, that help MVVM promote unit testing. We'll concentrate on the basics of the pattern in this discussion because frameworks can widely vary in their implementations.

In this module, we'll learn the parts of the MVVM pattern and the responsibilities of the model, view, and viewmodel. We'll then write code in a viewmodel to handle application behavior in a way that's isolated from the UI libraries (even from Xamarin.Forms itself).

Note

This module requires Visual Studio 2019. You can use either Windows or a Mac:

  • Windows: Make sure you have the Mobile development with .NET workload installed in Visual Studio 2019 for Windows. Refer to the documentation if you need to add it to your installation.
  • macOS: The standard Visual Studio 2019 for Mac installation includes everything you need to build mobile apps with Xamarin.

Learning objectives

In this module, you will:

  • Identify when to use the Model-View-ViewModel pattern
  • Use a viewmodel to drive visual behavior
  • Improve code testability through data-bindable properties

Prerequisites

  • Visual Studio 2019 (any edition)
  • Familiarity with C# and .NET