The MVVM Pattern in .NET MAUI: The definitive guide to essential patterns, best practices 9781805125006

Dive into MVVM and .NET MAUI and master how to effectively apply the design pattern with the help of this practical guid

1,908 19 6MB

English Pages 386 Year 2023

Report DMCA / Copyright

DOWNLOAD EPUB FILE

Table of contents :
The MVVM Pattern in .NET MAUI
Foreword
Contributors
About the author
About the reviewer
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Conventions used
Get in touch
Share Your Thoughts
Download a free PDF copy of this book
Part 1: Key Concepts and Components
1
What Is the MVVM Design Pattern?
Technical requirements
Looking at MVVM’s core components
Model
View
ViewModel
Commands
Data binding
Separation of concerns matters
Maintainability
Testability
MVVM in action
The code-behind approach
Using MVVM
Common misconceptions about MVVM
There should be no code in the code-behind
The Model should exclusively be a DTO, a domain entity, or a POCO
The View and the ViewModel should not know each other
MVVM is too complex and used only for large applications
Summary
2
What is .NET MAUI?
Technical requirements
.NET MAUI in a nutshell
Reminiscent of Xamarin.Forms
Cross-platform UI and more
How does it work?
Native apps with .NET
.NET MAUI, another abstraction layer
Creating your first .NET MAUI app
Setting up your machine
Hello “Recipes!” app
Summary
Further reading
3
Data Binding Building Blocks in .NET MAUI
Technical requirements
Key components for data binding
Elements of data binding
BindableObject
BindableProperty
BindingContext
Data binding in practice
Binding modes and the INotifyPropertyChanged interface
Binding modes in .NET MAUI
INofityPropertyChanged
Different binding modes in action
Handling interactions with the ICommand interface
The ICommand interface
Putting it into action
Summary
Further reading
4
Data Binding in .NET MAUI
Technical requirements
ValueConverters and StringFormat
Creating and using ValueConverters
Fallbacks
TargetNullValue
FallbackValue
Element and relative binding
Element binding
Relative binding
Multi-bindings
Multi-binding StringFormat
IMultiValueConverter
Compiled bindings
Summary
Further reading
5
Community Toolkits
Technical requirements
The MVVM Toolkit
ObservableObject
RelayCommand
Source Generators
The .NET MAUI Community Toolkit
Converters
Behaviors
Other popular frameworks
Contributing to the community
Summary
Further reading
6
Working with Collections
Technical requirements
Using BindableLayout
What to show and how to show it
Dynamically choosing a DataTemplate at runtime
The ICollectionChanged interface
The ICollectionChanged interface and binding modes
Using the ObservableCollection
Working with CollectionView
Displaying grouped data
Selecting items
Incrementally loading data
Other common interactions
Summary
Further reading
Part 2: Building a .NET MAUI App Using MVVM
7
Dependency Injection, Services, and Messaging
Technical requirements
Inversion of Control through Dependency Injection
Dependency Injection
Registering, resolving, and injecting services
Registering services
Resolving and injecting services
Applying Dependency Injection
Messaging
WeakReferenceMessenger
Summary
Further reading
8
Navigation in MVVM
Technical requirements
About .NET MAUI Shell
Setting up Shell
Routing
Supporting Dependency Injection
Setting up navigation in a .NET MAUI Shell app
Creating an INavigationService interface
Creating and using a NavigationService
Passing parameters
Hooking into navigation
Setting up navigation without .NET MAUI Shell
Passing results back
Summary
Further reading
9
Handling User Input and Validation
Technical requirements
Implementing input validation on ViewModels
Using the ObservableValidator
Visualizing validation errors with triggers
Prompts and alerts
Confirming or canceling navigation
Summary
Further reading
10
Working with Remote Data
Technical requirements
Revisiting the model architecture
Updates to the codebase
Always return a result
Adding repository interfaces
API communication with Refit
Getting started with Refit
Creating API interfaces
Integrating Refit
API communication from ViewModels
Showing a loading indicator
Handling failures
Summary
Further reading
Part 3: Mastering MVVM Development
11
Creating MVVM-Friendly Controls
Technical requirements
Building a custom control with bindable properties
Implementing FavoriteControl
Animating state changes
Enabling interactions on custom controls
User actions and reflecting state
Adding command-based interactions
Working with control templates
Accessing elements from a template
Summary
Further reading
12
Localization with MVVM
Technical requirements
Working with cultures, resource files, and localization
Getting the user’s culture
Setting a different culture
Displaying formatted data
What are Resources Files?
Getting localized resources on the screen
Looking at a localization solution for MVVM
The Localization project
The Localization.Maui project
Using the LocalizedResourcesProvider and LocalizationManager
Using a custom Translate markup extension
Fetching localized data from APIs
Summary
Further reading
13
Unit Testing
Technical requirements
The importance of unit testing
Setting up a unit test project
Creating a unit test project
Creating unit tests
Looking at data-driven tests
Generating data with Bogus
Mocking dependencies with Moq
Applying mocking in our ViewModel tests
Thin UI, deep tests
Testing MAUI-specific code
Summary
Further reading
14
Troubleshooting and Debugging Tips
Technical requirements
Common data binding issues
Checking the Output and XAML Binding Failures window
Debugging with a DoNothingConverter
Troubleshooting collections
The data binding pitfall on Behaviors
Services and Dependency Injection pitfalls
Unable to resolve service for type
No parameterless constructor defined for type
Incorrect service registration
Frequent custom control and converter problems
Troubleshooting bindable properties
Binding to the BindingContext
Value converter issues
Summary
Further reading
Index
Why subscribe?
Other Books You May Enjoy
Packt is searching for authors like you
Share Your Thoughts
Download a free PDF copy of this book

The MVVM Pattern in .NET MAUI: The definitive guide to essential patterns, best practices
 9781805125006

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
Recommend Papers