.NET MAUI for C# Developers

Book description

A practical guide to mastering C# and .NET MAUI to build native cross-platform apps with a single codebase Purchase of the print or Kindle book includes a free PDF eBook

Key Features

  • Extend your skills to build cross-platform native apps for iOS, Android, Windows, and Mac
  • Learn by working on a real-world project
  • Go beyond MAUI and integrate with the .NET ecosystem

Book Description

While UI plays a pivotal role in retaining users in a highly competitive landscape, maintaining the same UI can be tricky if you use different languages for different platforms, leading to mismatches and un-synced pages. In this book, you'll see how .NET MAUI allows you to create a real-world application that will run natively on different platforms. By building on your C# experience, you’ll further learn to create beautiful and engaging UI using XAML, architect a solid app, and discover best practices for this Microsoft platform.

The book starts with the fundamentals and quickly moves on to intermediate and advanced topics on laying out your pages, navigating between them, and adding controls to gather and display data. You’ll explore the key architectural pattern of Model-View-ViewModel: and ways to leverage it. You’ll also use xUnit and NSubstitute to create robust and reliable code.

By the end of this book, you’ll be well-equipped to leverage .NET MAUI and create an API for your app to interact with a web frontend to the backend data using C#.

What you will learn

  • Explore the fundamentals of creating .NET MAUI apps with Visual Studio
  • Understand XAML as the key tool for building your user interface
  • Obtain and display data using layout and controls
  • Discover the MVVM pattern to create robust apps
  • Acquire the skills for storing and retrieving persistent data
  • Use unit testing to ensure your app is solid and reliable

Who this book is for

Learning .NET MAUI is targeted squarely at C# programmers, whether or not they have experience with cross-platform or mobile applications, who wish to learn Microsoft's premier platform for building iOS, Android, Windows and Mac apps using a single C#-based solution.

Table of contents

  1. .NET MAUI for C# Developers
  2. Foreword
  3. Contributors
  4. About the authors
  5. About the reviewers
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
    9. Download a free PDF copy of this book
  7. Part 1 – Getting Started
  8. Chapter 1: Assembling Your Tools and Creating Your First App
    1. Technical requirements
    2. Getting and installing Visual Studio
      1. Installing Visual Studio
    3. Git
    4. Opening Visual Studio
      1. Quick tour of the app
    5. Summary
    6. Quiz
    7. You try it
  9. Chapter 2: What We Will Build: Forget Me Not
    1. Technical requirements
    2. What is Forget Me Not?
    3. Buddies
      1. Inviting Buddies
    4. Other pages
    5. What you’ll learn
    6. Summary
  10. Chapter 3: XAML and Fluent C#
    1. Technical requirements
    2. Understanding the structure of XAML
      1. The code-behind file
    3. Exploring the layout options
      1. VerticalStackLayout
      2. Image
      3. Label
      4. Code-behind and event handlers
    4. If you can do it in XAML, you can do it in C#
      1. C# versus Fluent C#
    5. Summary
    6. Quiz
    7. Try it out
  11. Chapter 4: MVVM and Controls
    1. Technical requirements
    2. Setting up for MVVM
      1. Creating folders
      2. The MVVM Community Toolkit
      3. Exploring views
      4. Forget Me Not labels
    3. Data binding
      1. Creating a public property
      2. Setting up BindingContext
      3. Assigning values to the View Model class properties
      4. Implementing Binding
      5. ViewModel versus code-behind
    4. Views
      1. Images
      2. Button properties
      3. ImageButton
      4. TapGestureRecognizer
      5. Entering text
    5. Behaviors
    6. Popups and dialogs
      1. Presenting the user with a choice
      2. ActionSheet
      3. Displaying a prompt
      4. Toast
      5. Snackbar
      6. BoxView
      7. Frame
    7. Brushes
      1. The Solid brush
      2. LinearGradientBrush
      3. RadialGradientBrush
    8. Summary
    9. Quiz
    10. You try it
  12. Chapter 5: Advanced Controls
    1. Technical requirements
    2. Keeping the user informed of activity
      1. ActivityIndicator
      2. ProgressBar
    3. Moving event handling to ViewModel
      1. Breaking it down
    4. Sending and receiving messages
      1. Getting started with WeakReferenceMessenger
    5. Creating the page in C#
    6. Displaying collections
      1. Overriding OnAppearing
      2. Understanding how the service works
      3. Displaying the collection of Preference objects
      4. The code-behind
    7. Styles
      1. Explicit versus implicit styles
      2. Style inheritance or BasedOn
    8. Summary
    9. Quiz
    10. You try it
  13. Chapter 6: Layout
    1. Technical requirements
    2. Stack layouts
    3. Grid
      1. Sizing rows and columns
      2. Named rows and columns
    4. ScrollView
    5. FlexLayout
    6. Summary
    7. Quiz
    8. You try it
  14. Part 2 – Intermediate Topics
  15. Chapter 7: Understanding Navigation
    1. Technical requirements
    2. Exploring the TabBar
    3. Creating the About and Buddies pages
      1. Assembling the About page
    4. Shell navigation
    5. Routing
    6. Passing values from page to page
      1. Passing values with the url (?) syntax
      2. Passing values with a dictionary
    7. Summary
    8. Quiz
    9. You try it
  16. Chapter 8: Storing and Retrieving Data
    1. Technical requirements
    2. Storing user preferences
      1. UserPreferencesViewModel
      2. Navigating to UserPreferences
      3. Retrieving the preferences
    3. Storing to a database on your device
      1. Installing SQLite
      2. Getting started with SQLite
      3. The Database class
      4. CRUD
    4. Local or remote?
    5. Summary
    6. Quiz
    7. You try it
  17. Chapter 9: Unit Testing
    1. Technical requirements
    2. Why create unit tests?
      1. Vote early and vote often
    3. Creating unit tests
      1. Setting the project reference
      2. Creating the first unit test
      3. Tweaking the project file
      4. Running the test
    4. ForgetMeNotDemo unit tests
      1. Implementing the triple-A pattern
      2. What’s wrong with this test?
    5. Mocks
    6. Dependency injection
      1. Creating an interface
      2. Modifying the class constructor
      3. The .NET MAUI IoC container
      4. Registering your interfaces, services, and ViewModels
    7. Using the NSubstitute package
      1. Adding NSubstitute to your test fixture
      2. Testing corner cases
    8. Summary
    9. Quiz
    10. You try it
  18. Part 3 – Advanced Topics
  19. Chapter 10: Consuming REST Services
    1. Technical requirements
    2. Using REST Services
    3. The Forget Me Not API architecture
      1. Creating the projects
      2. Fleshing out the models
    4. Examining the API domain objects
    5. Reviewing DTOs
      1. Other DTO files
    6. Understanding ForgetMeNot.APIClient
      1. Authentication
      2. Profile
      3. The Buddy region
    7. Using the API
      1. Creating the account
      2. Modifying the Login page
      3. Updating LoginPage
      4. The AccountService class
      5. Updating LoginViewModel
      6. Using AccountService to log in
      7. Setting up the Create Account page
      8. Setting up CreateAccountViewModel
      9. Handling the SignUp command
      10. What to do if it won’t build
    8. Summary
    9. Quiz
    10. You try it
  20. Chapter 11: Exploring Advanced Topics
    1. Technical requirements
    2. Selecting data templates at runtime
      1. Declaring ItemTemplates as resources
      2. The DataTemplate selection
      3. The DataTemplateSelector class
      4. Adding the template selector to the page’s resources
      5. Adding DataTemplateSelector to CollectionView
    3. Managing Visual State
      1. Defining the common visual states
      2. A button VisualState example
    4. Utilizing Community Toolkit behaviors
    5. Taking action with triggers
    6. Summary
    7. Quiz
    8. You try it
  21. Assessments
    1. Chapter 1, Assembling Your Tools and Creating Your 
First App
    2. Chapter 3, XAML and Fluent C#
    3. Chapter 4, MVVM and Controls
    4. Chapter 5, Advanced Controls
    5. Chapter 6, Layout
    6. Chapter 7, Understanding Navigation
    7. Chapter 8, Storing and Retrieving Data
    8. Chapter 9, Unit Testing
    9. Chapter 10, Consuming REST Services
    10. Chapter 11, Exploring Advanced Topics
  22. Index
    1. Why subscribe?
  23. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: .NET MAUI for C# Developers
  • Author(s): Jesse Liberty, Rodrigo Juarez
  • Release date: March 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781837631698