Reader small image

You're reading from  .NET MAUI Cross-Platform Application Development - Second Edition

Product typeBook
Published inMar 2024
Reading LevelBeginner
PublisherPackt
ISBN-139781835080597
Edition2nd Edition
Languages
Right arrow
Author (1)
Roger Ye
Roger Ye
author image
Roger Ye

Roger Ye is an embedded system programmer who has great interest in virtualization, Android, and cross-platform technologies. His professional experience includes working with major companies like Motorola, Emerson, Intel, and EPAM, where he held the position of Engineering Manager. At Motorola and Emerson, he worked on embedded system projects for mobile devices and telecommunication infrastructures. He is now an engineering manager at EPAM, working with a team to deliver digital solutions for diverse clients.
Read more about Roger Ye

Right arrow

Building Our First .NET MAUI App

In this chapter, we will create a new .NET MAUI project and customize it so that we can use it in the development of an app. The app that we will develop is a password management app. We will gradually introduce various features to it in the subsequent chapters. By the end of Part 1, we will possess a fully functional password management app.

Those who have prior experience with Xamarin.Forms will recall that the Shell serves as a convenient application container that simplifies app development by offering a unified structure for defining an application’s key components. While there isn’t a direct Visual Studio template for .NET MAUI Shell from Microsoft, we can effectively utilize the one from Xamarin.Forms. To incorporate Shell in our app, we’ll reuse the project template found in Xamarin.Forms. As well as from providing us with a proficient project template, the process of migrating the Xamarin.Forms Shell template to .NET...

Technical requirements

To test and debug the source code in this chapter, you need Visual Studio 2022 installed on either Windows or macOS. Please refer to the Development environment setup section in Chapter 1, Getting Started with .NET MAUI, for details.

To check out the source code of this chapter, we can use the following Git command:

$ git clone https://github.com/PacktPublishing/.NET-MAUI-Cross-Platform-Application-Development-Second-edition.git -b 2nd/chapter02

Managing the source code in this book

Since we will develop a password manager app incrementally in this book, the source code of each chapter is built on top of the previous chapters. To continuously improve our app, we will have separate branches for the source code of each chapter. If you want to clone the source code of all chapters in one command, you can clone it from the main branch. In the main branch, all the chapters are in separate folders. If you don’t want to use Git, you can also download the source code as a compressed file from the release area, as shown in the following screenshot (Figure 2.1):

Figure 2.1: Source code in GitHub

Figure 2.1: Source code in GitHub

Since new .NET MAUI releases may be available from time to time, the Git tags and versions in the release area will be updated according to the new .NET MAUI releases and bug fixes.

The source code of this book can be found in the following GitHub repository: https://github.com/PacktPublishing/.NET-MAUI-Cross-Platform...

Setting up a new .NET MAUI project

In this chapter, we will create and configure a new .NET MAUI project, which will form the basis for further development of our password manager app. Given that the default .NET NAUI project template is a very simple one, we require a more robust project framework to establish the base project structure.

Xamarin.Forms project templates provide suitable options. In particular, there is a template that incorporates the Shell and Model-View-View Model (MVVM) pattern setup. We’ll migrate this into our .NET MAUI project, which will also provide us with the opportunity to learn how to migrate a Xamarin.Forms project to .NET MAUI. Ultimately, we’ll create our very own Visual Studio project template.

To create a new .NET MAUI project, we can use Visual Studio or the command line.

Creating a new project using Visual Studio

To create a new .NET MAUI project, follow these steps:

  1. Launch Visual Studio 2022 and select Create...

App startup and lifecycle

Lifecycle management in .NET MAUI is crucial for efficient resource management, ensuring smooth and consistent user experiences, secure application handling, and understanding and troubleshooting app behavior. It allows the application to conserve resources, appropriately saving and restoring the application’s state when it’s in the background or foreground. It provides opportunities to perform certain actions when an app goes to the background, such as saving data or pausing activities. Moreover, it provides enhanced security by managing sensitive data when apps switch to and from the active state. Hence, understanding the application lifecycle is crucial for crafting robust, efficient, and user-friendly .NET MAUI applications.

In .NET MAUI projects, app startup and lifecycle management are handled in the following two files:

  • MauiProgram.cs
  • App.xaml/App.xaml.cs

.NET Generic Host is used for app startup and configuration...

Configuring the resources

Resource management is one of the major differences between .NET MAUI and Xamarin.

Cross-platform development presents unique challenges as each platform has its own method for managing resources. This diversity can pose significant management tasks for development teams. For instance, we must incorporate multiple image sizes to accommodate various resolutions.

In Xamarin, most of the resources are managed separately in platform-specific projects. If we want to add an image, we must add the image files with different sizes to all platform projects separately.

.NET MAUI provides an elegant solution to manage resources effectively. The design goal of one single project for all supported platforms helps to manage resources in one place.

In .NET MAUI, resource files can be tagged into different categories using a build action based on the role they play in the project, as we can see in Table 2.2:

Resource...

Building and debugging

As we mentioned in Chapter 1, Getting Started with .NET MAUI, we cannot build and test every target using a single platform. Please refer to Table 1.8 for the available build targets on Windows and macOS platforms. For the sake of simplicity, we will build and test targets Windows and Android on Windows. For iOS and macOS builds, we will do it on the macOS platform.

Once we’ve set things up, we can start building and debugging our application.

Let’s begin with building and testing on the Windows platform. We can choose the framework that we want to run or debug, depending on our needs, as depicted in Figure 2.6:

Figure 2.6: Building and debugging

Windows

We can run or debug a Windows build on a local machine by choosing net8.0-windows10.0.19041.0 as the framework. However, to accomplish this, we must first enable Developer Mode on Windows, if it’s not yet been activated. Please refer to Figure 2.7 for guidance on...

Migrating from Xamarin

In this section, we will showcase the process of migrating a Xamarin.Forms project template to .NET MAUI. It should be noted that this serves only as an example, given that there are many types of Xamarin projects. Therefore, only an example of migrating a Xamarin.Forms project to .NET MAUI is discussed in this chapter. For additional information on migrating Xamarin-native projects and other related topics, please refer to the Microsoft documentation: https://learn.microsoft.com/en-us/dotnet/maui/migration/.

Though we are primarily porting the Xamarin.Forms Shell template as an example in this chapter, this new project template is crucial for our subsequent developments. To provide further context, I will briefly introduce the planned migration of PassXYZ.Vault from Xamarin.Forms to .NET MAUI. This will outline the challenges we’ll need to tackle in this book, which should prepare you for the challenges you may encounter when migrating your own apps...

Summary

In this chapter, we created a new .NET MAUI project. We learned how to configure our .NET MAUI app using .NET Generic Host and adjusted the resources configuration to utilize a custom font (Font Awesome). We also learned about the .NET MAUI application lifecycle and tested the process of subscribing to lifecycle events by overriding the CreateWindow method and by creating a derived class of the Window class. To generate boilerplate code with MVVM pattern and Shell support, we created a new .NET MAUI project template. This walkthrough served to demonstrate how to migrate Xamarin.Forms code to .NET MAUI.

In our next chapter, we will learn how to create a user interface using XAML, which can be used to build user interfaces for WPF, UWP, Xamarin.Forms, and .NET MAUI. We will continue to create and enhance the user interfaces of our password manager app using XAML.

Learn more on Discord

Join our community’s Discord space for discussions with the author and other...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
.NET MAUI Cross-Platform Application Development - Second Edition
Published in: Mar 2024Publisher: PacktISBN-13: 9781835080597
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Roger Ye

Roger Ye is an embedded system programmer who has great interest in virtualization, Android, and cross-platform technologies. His professional experience includes working with major companies like Motorola, Emerson, Intel, and EPAM, where he held the position of Engineering Manager. At Motorola and Emerson, he worked on embedded system projects for mobile devices and telecommunication infrastructures. He is now an engineering manager at EPAM, working with a team to deliver digital solutions for diverse clients.
Read more about Roger Ye