Interested in giving Microsoft’s new Multi-platform App UI (MAUI) a try? This book provides developers with a comprehensive set of tools and hands-on coding to build their own cross-platform applications.
The book is a comprehensive end-to-end guide on creating, building, and distributing .NET MAUI applications. As you walk through each new concept, .NET MAUI expert and Microsoft MVP Shaun Lawrence will show the concept's value and uses, and then have you apply it in a build-along application. You will work with this build-along application right up until you are ready to ship it to the relevant stores (e.g., App Store, etc.).
The underlying theme through the book is to explain a key concept, show how to implement it directly in order to reinforce your understanding, and then show potential simplifications (e.g., alternative libraries) that can help you reduce the complexities of an applications code base.
What You Will Learn
Create a .NET MAUI application
Apply commonly required techniques
Apply more advanced techniques to make applications stand out
Know what to consider when distributing applications to the relevant stores
Take an application from concept through to production
Who This Book Is For
Developers who are new to .NET MAUI and cross-platform development. A basic knowledge of C# is required, but no prior knowledge using .NET MAUI is required. Experienced developers also will find the book useful as it covers topics key to modern day development (such as accessibility, performance, and distribution). Xamarin Forms developers will find the book useful as key features new to .NET MAUI that will aid in the migration from Xamarin Forms to .NET MAUI are explained.
Author(s): Shaun Lawrence
Edition: 1
Publisher: Apress
Year: 2023
Language: English
Pages: 471
Tags: C# .NET MAUI Programming
Table of Contents
About the Author
About the Technical Reviewer
Acknowledgments
Introduction
Chapter 1: Introduction to .NET MAUI
What is .NET MAUI?
Digging a Bit Deeper
Where Did It Come From?
How It Differs From the Competition
Why Use .NET MAUI?
Supported Platforms
Code Sharing
Developer Freedom
Community
Fast Development Cycle
.NET Hot Reload
XAML Hot Reload
Performance
Strong Commercial Offerings
Limitations of .NET MAUI
No Web Assembly (WASM) Support
No Camera API
Apps Won’t Look Identical on Each Platform
Lack of Media Playback Out of the Box
The Glass Is Half Full, Though
How to Build .NET MAUI Applications
Visual Studio
Visual Studio (Windows)
Visual Studio for Mac
Rider
Visual Studio Code
Summary
Chapter 2: Building Our First application
Setting Up Your Environment
macOS
Visual Studio for Mac
Xcode
Remote Access
Windows
Visual Studio
Visual Studio to macOS
Troubleshooting Installation Issues
.NET MAUI Workload Is Missing
Visual Studio Installer
Command Line
Creating Your First Application
Creating in Visual Studio
Creating in the Command Line
Building and Running Your First Application
Getting to Know Your Application
WidgetBoard
Summary
Source Code
Chapter 3: The Fundamentals of .NET MAUI
Project Structure
/Platforms/ Folder
Android
iOS
MacCatalyst
Tizen
Windows
Summary
/Resources/ Folder
Fonts
Images
Raw
Where To Begin?
Generic Host Builder
What Is Dependency Injection?
Registering Dependencies
AddSingleton
AddTransient
AddScoped
Application Lifecycle
Application States
Lifecycle Events
Handling Lifecycle Events
Cross-Platform Mappings to Platform Lifecycle Events
Platform-Specific Lifecycle Events
Android
iOS and MacCatalyst
Windows
Summary
Chapter 4: An Architecture to Suit You
A Measuring Stick
Prerequisites
Model View ViewModel (MVVM)
Model
View
XAML
C# (Code-Behind)
ViewModel
Model View Update (MVU)
Getting Started with Comet
Adding Your MVU Implementation
XAML vs. C# Markup
Plain C#
C# Markup
Chosen Architecture for This Book
Adding the ViewModels
Adding IWidgetViewModel
Adding BaseViewModel
Adding ClockWidgetViewModel
Adding Views
Adding IWidgetView
Adding ClockWidgetView
Viewing Your Widget
Modifying MainPage.xaml
Modifying MainPage.xaml.cs
Taking the Application for a Spin
MVVM Enhancements
MVVM Frameworks
Magic
Summary
Source Code
Chapter 5: User Interface Essentials
Prerequisites
Models
BaseLayout.cs
FixedLayout.cs
Board.cs
Pages
BoardDetailsPage
FixedBoardPage
ViewModels
AppShellViewModel
BoardDetailsPageViewModel
FixedBoardPageViewModel
App Icons
Adding Your Own Icon
Platform Differences
Android
iOS and macOS
Splash Screen
XAML
Dissecting a XAML File
Building Your First XAML Page
Layouts
AbsoluteLayout
FlexLayout
Grid
HorizontalStackLayout
VerticalStackLayout
Data Binding
Binding
BindingContext
Path
Mode
Source
Applying the Remaining Bindings
MultiBinding
Command
Compiled Bindings
Shell
ShellContent
Navigation
Registering Pages for Navigation
Performing Navigation
Navigating Backwards
Passing Data When Navigating
Flyout
FlyoutHeader
FlyoutContent
Collection of Boards
Selected Board
Navigation to the Selected Board
Setting the BindingContext of Your AppShell
Register AppShell with the MAUI App Builder
Resolve the AppShell Instead of Creating It
Tabs
Search
Taking Your Application for a Spin
Summary
Source Code
Extra Assignment
Chapter 6: Creating Our Own Layout
Placeholder
ILayoutManager
BoardLayout
BoardLayout.xaml
BindableLayout
BoardLayout.xaml.cs
Adding the LayoutManager Property
Adding the ItemsSource Property
Adding the ItemTemplateSelector Property
Handling the ChildAdded Event
Adding Remaining Bits
FixedLayoutManager
Accepting the Number of Rows and Columns for a Board
Adding the NumberOfColumns Property
Adding the NumberOfRows Property
Providing Tap/Click Support Through a Command
Building the Board Layout
Setting the Correct Row/Column Position for Each Widget
Using Your Layout
Adding a Factory That Will Create Instances of Your Widgets
Allowing for the Registration of Widget Views and View Models
Creation of a Widget View
Creation of a Widget View Model
Registering the Factory with MauiAppBuilder
Registering Your ClockWidget with the Factory
WidgetTemplateSelector
Registering the Template Selector with MauiAppBuilder
Updating FixedBoardPageViewModel
Finally Using the Layout
Summary
Source Code
Extra Assignment
Chapter 7: Accessibility
What Is Accessibility?
Why Make Your Applications Accessible?
What to Consider When Making Your Applications Accessible
How to Make Your Application Accessible
Screen Reader Support
SemanticProperties
SemanticProperties.Description
SemanticProperties.Hint
SemanticProperties.HeadingLevel
SemanticScreenReader
AutomationProperties
AutomationProperties.ExcludedWithChildren
AutomationProperties.IsInAccessibleTree
Suitable Contrast
Dynamic Text Sizing
Avoiding Fixed Sizes
Preferring Minimum Sizing
Font Auto Scaling
Testing Your Application’s Accessibility
Android
iOS
macOS
Windows
Accessibility Checklist
Summary
Source Code
Extra Assignment
Chapter 8: Advanced UI Concepts
Adding the Ability to Add a Widget to a Board
Possible Ways of Achieving Your Goal
Showing a Modal Page
Overlaying a View
Showing a Popup
The Chosen Approach
Adding Your Overlay View
Updating Your View Model
Showing the Overlay View
Styling
Examining the Default Styles
TargetType
ApplyToDerivedTypes
Setter
Creating a Style
AppThemeBinding
Further Reading
Triggers
Creating a DataTrigger
EnterActions and ExitActions
Creating a TriggerAction
Creating ShowOverlayTriggerAction
Using ShowOverlayTriggerAction
Further Reading
Animations
Basic Animations
Combining Basic Animations
Chaining Animations
Concurrent Animations
Cancelling Animations
Easings
Complex Animations
Recreating the ScaleTo Animation
Creating a Rubber Band Animation
Combining Triggers and Animations
Summary
Source Code
Extra Assignment
Animate the BoxView Overlay
Animate the New Widget
Chapter 9: Local Data
What Is Local Data?
File System
Cache Directory
App Data Directory
Database
Repository Pattern
Creating a Board
Listing Your Boards
Loading a Board
SQLite
Installing SQLite-net
Using Sqlite-net
Connecting to an SQLite database
Mapping Your Models
Creating Your Tables
Inserting into an SQLite Database
Reading a Collection from an SQLite Database
Reading a Single Entity from an SQLite Database
Deleting from an SQLite Database
Updating an Entity in an SQLite Database
LiteDB
Installing LiteDB
Using LiteDB
Connecting to a LiteDB database
Mapping Your Models
Creating Your Tables
Inserting into a LiteDB Database
Reading a Collection from a LiteDB Database
Reading a Single Entity from a LiteDB Database
Deleting from a LiteDB Database
Updating an Entity in a LiteDB Database
Database Summary
Application Settings (Preferences)
What Can Be Stored in Preferences?
Setting a Value in Preferences
Getting a Value in Preferences
Checking if a Key Exists in Preferences
Removing a Preference
Secure Storage
Storing a Value Securely
Reading a Secure Value
Removing a Secure Value
Platform specifics
Android
iOS and macOS
Windows
Viewing the Result
Summary
Source Code
Extra Assignment
Chapter 10: Remote Data
What Is Remote Data?
Considerations When Handling Remote Data
Loading Times
Failures
Security
Webservices
The Open Weather API
Creating an Open Weather Account
Creating an Open Weather API key
Examining the Data
Using System.Text.Json
Creating Your Models
Connecting to the Open Weather API
Creating the WeatherWidgetView
Creating the WeatherWidgetViewModel
Registering Your Widget
Testing Your Widget
Adding Some State
Converting the State to UI
Displaying the Loading State
Displaying the Loaded State
Displaying the Error State
Simplifying Webservice Access
Prebuilt Libraries
Code Generation Libraries
Adding the Refit NuGet Package
Further Reading
Polly
StateContainer from CommunityToolkit.Maui
Summary
Source Code
Extra Assignment
TODO Widget
Quote of the Day Widget
NASA Space Image of the Day Widget
Chapter 11: Getting Specific
.NET MAUI Essentials
Permissions
Checking the Status of a Permission
Requesting Permission
Handling Permissions in Your Application
Using the Geolocation API
Registering the Geolocation Service
Using the Geolocation Service
Registering the LocationService
Using the ILocationService
Displaying Permission Errors to Your User
Configuring Platform-Specific Components
Android
iOS/Mac
Windows
Platform-Specific API Access
Platform-Specific Code with Compiler Directives
Platform-Specific Code in Platform Folders
Overriding the Platform-Specific UI
OnPlatform
OnPlatform Markup Extension
Conditional Statements
Handlers
Customizing Controls with Mappers
Scoping of Mapper Customization
Further Reading
Summary
Source Code
Extra Assignment
Barometer Widget
Geocoding Lookup
Chapter 12: Testing
Unit Testing
Unit Testing in .NET MAUI
xUnit
NUnit
MSTest
Your Chosen Testing Framework
Adding Your Own Unit Tests
Adding a Unit Test Project to Your Solution
Modify Your Application Project to Target net7.0
Adding a Reference to the Project to Test
Modify Your Test Project to Use MAUI Dependencies
Testing Your View Models
Testing BoardDetailsPageViewModel
Testing INotifyPropertyChanged
Testing Asynchronous Operations
Creating Your ILocationService Mock
Creating Your WeatherForecastService Mock
Creating Your Asynchronous Tests
Testing Your Views
Creating Your ClockWidgetViewModel Mock
Creating Your View Tests
Device Testing
Creating a Device Test Project
Adding a Device-Specific Test
Running Device-Specific Tests
Snapshot Testing
Snapshot Testing Your Application
Passing Thoughts
Looking to the Future
Summary
Source Code
Chapter 13: Lets Get Graphical
.NET MAUI Graphics
Drawing on the Screen
Updating the Surface
Drawing a Line
Drawing a Path
Maintaining the State of the Canvas
Further Reading
Building a Sketch Widget
Creating the SketchWidgetViewModel
Representing a User Interaction
Creating the SketchWidgetView
Modifying the SketchWidgetView.xaml
Modifying the SketchWidgetView.xaml.cs
Registering Your Widget
Taking Your Widget for a Test Draw
Summary
Source Code
Extra Assignment
Chapter 14: Releasing Our Application
Distributing Your Application
Android
Additional Resources
iOS
Certificate
Identifier
Capabilities
Entitlements
Provisioning Profiles
Additional Resources
macOS
Additional Resources
Windows
Additional Resources
Things to Consider
Following Good Practices
Performance
Startup Tracing
Image Sizes
Use of ObservableCollection
Linking
What Is Linking?
Issues That Crop Up
Crashes/Analytics
Sentry
App Center
Obfuscation
Distributing Test Versions
Summary
Chapter 15: Conclusion
Looking at the Final Product
Taking the Project Further
Useful Resources
StackOverflow
GitHub
YouTube
Gerald Versluis
James Montemagno
Social Media
Yet More Goodness
Looking Forward
Upgrading from Xamarin.Forms
Comet
Testing
Index