Book Image

The MVVM Pattern in .NET MAUI

By : Pieter Nijs
Book Image

The MVVM Pattern in .NET MAUI

By: Pieter Nijs

Overview of this book

In today's fast-paced world of modern software development, teams need to be efficient, productive, and capable of rapidly adapting to changes to deliver high-quality products, making it crucial for developers to write maintainable and easy-to-test code. The MVVM Pattern in .NET MAUI helps you to thoroughly explore the Model-View-View Model (MVVM) design pattern. The chapters show you how this pattern helps in structuring code to embrace the separation of concerns, allowing for loosely coupled user interface and application logic, which ultimately empowers you to write more robust, maintainable, and testable code. The book also highlights .NET MAUI's capabilities and features, and enables you to delve into the essential components within the framework that facilitate the application of the MVVM pattern. With the help of a sample application, this definitive guide takes a hands-on approach to walk you through both the essential and advanced usages of the MVVM pattern to ensure that you successfully apply the practical aspects of the pattern to your .NET MAUI projects. By the end of this book, you’ll have gained a comprehensive understanding of the MVVM design pattern and its relevance in the context of .NET MAUI, as well as developed the skills needed to successfully apply it in practice.
Table of Contents (20 chapters)
Free Chapter
1
Part 1: Key Concepts and Components
8
Part 2: Building a .NET MAUI App Using MVVM
13
Part 3: Mastering MVVM Development

Fallbacks

There are occasions where data binding can fail; the binding source cannot be resolved (yet) or the returned value is null. Although ValueConverters and additional code could tackle many such situations, we can also enhance the resilience of our data bindings by setting fallback values. This can easily be done by setting the TargetNullValue or FallbackValue property in our binding expression.

TargetNullValue

The TargetNullValue property can be used in situations where we want to handle the situation where the resolved binding source target returns null. In other words, the binding engine can resolve the bound property, but this property returns a null value.

In our app, the Calories property on RecipeDetailViewModel is defined as a nullable int. This makes it essential for us to handle any potential null values in our data binding elegantly. If we leave the binding statement as-is, the label would show "Calories: kcal" if the Calories property is null...