Reader small image

You're reading from  .NET MAUI Projects - Third Edition

Product typeBook
Published inFeb 2024
Reading LevelN/a
PublisherPackt
ISBN-139781837634910
Edition3rd Edition
Languages
Right arrow
Authors (3):
Michael Cummings
Michael Cummings
author image
Michael Cummings

Michael Cummings is a Senior Development Engineer with Microsoft. He currently works on the Visual Studio tooling for building WPF, Xamarin.Forms, and .NET MAUI apps. He has experience as a developer and architect with concentrations in app development, design, deployment, and business process as it relates to technology. Michael has been a graphics and game programmer since the days of the TI99-4/A. He started the Boston XNA Developers Group. He has contributed to many open-source projects, including AXIOM, a .NET/Mono 3D rendering engine, and Planeshift, a 3D MMORPG. Michael also has experience with native game and graphics development (C++/DirectX) on Windows. Michael lives in Lexington, MA with his wife and their dog. When not working with technology he enjoys watching movies, trying out new recipes, and the occasional game of full-contact racquetball.
Read more about Michael Cummings

Daniel Hindrikes
Daniel Hindrikes
author image
Daniel Hindrikes

Daniel Hindrikes is a developer and architect with a passion for developing mobile apps powered by the cloud. Daniel fell in love with Xamarin in the early days of Xamarin when he realized that he could use C# even for iOS and Android apps, and that he could share code with the Windows apps he was also building. But Daniel started to build mobile apps long before that, working on Android apps with Java and even Java ME apps (a long, long time ago). Daniel enjoys sharing his knowledge and can be found speaking at conferences, blogging, or recording the podcast, The Code Behind. Daniel works at the company tretton37 in Sweden and has experience working with both local and global customers.
Read more about Daniel Hindrikes

Johan Karlsson
Johan Karlsson
author image
Johan Karlsson

Johan Karlsson has been working with Xamarin since the days of MonoTouch and Mono for Android, and it all started with writing a game. He is a full-stack developer, currently focusing on mobile applications using Xamarin, but has in the past worked a lot with ASP.NET MVC, Visual Basic.NET (not proud), and C# in general. Also, he's created a whole bunch of databases in SQL Server over the years. Johan works at tretton37 in Sweden and has about 20 years' experience in the trade of assembling ones and zeros.
Read more about Johan Karlsson

View More author details
Right arrow

Converting a Xamarin.Forms App into .NET MAUI

Before we dive too far into .NET MAUI, we will look back at an existing Xamarin.Forms app and convert it into .NET MAUI. This chapter will guide you through the steps to convert an existing Xamarin.Forms app running on Mono into a .NET MAUI app running on .NET 7. We will discuss two different methods for converting your Xamarin.Forms application into .NET MAUI. The first method will use a new .NET MAUI project and move our old Xamarin.Forms code into the new project. The second method will use the .NET Upgrade Assistant tool to do some of the upgrades for us.

If you are new to .NET MAUI and are not coming from Xamarin.Forms app development, feel free to skip this chapter and go straight to the next project.

The following topics will be covered in this chapter:

  • Moving code to a new .NET MAUI project
  • An overview of upgrading a Xamarin.Forms app
  • Installing and running .NET Upgrade Assistant

Technical requirements

To complete this project, you need to have Visual Studio installed on your Macintosh (Mac) or PC, as well as the .NET Mobile components. Refer to Chapter 1, Introduction to .NET MAUI, for more details on how to set up your environment. There are additional components that will be installed in this chapter, so you will need an internet connection to download and install .NET Upgrade Assistant. This chapter provides screenshots and instructions for Visual Studio on Windows.

This chapter will both be a classic File | New | Project chapter and use an existing app, guiding you step by step through the process of migrating an app from Xamarin.Forms to .NET MAUI. For the second app, you will need to download the source from this book’s GitHub repository.

You can find the full source for the code in this chapter at https://github.com/PacktPublishing/MAUI-Projects-3rd-Edition under the Chapter03 folder.

Project overview

This chapter is not meant to be an exhaustive tome of all the things you need to be aware of when converting your Xamarin.Forms app into .NET MAUI. Rather, it is an overview of what you need to consider when migrating your app, along with two walk-throughs of methods to accomplish that task. There are too many variations in application styles, versions, frameworks, custom controls, and so on for this one chapter to cover every scenario. That could take an entire book and would most likely be outdated by the time it was published. So, in this chapter, we are going to focus on a simple migration method that has the benefit of using the .NET MAUI Single Project feature and .NET Migration Assistant, which will automate much of the manual stuff and is constantly being updated.

The first part of this chapter will use a new Xamarin.Forms app created from the Shell template. The second part of this chapter will use .NET Upgrade Assistant to upgrade the open source app,...

Migrating into a blank .NET MAUI template

This method of moving your existing app code into a new .NET MAUI app is used mainly for smaller, simpler apps that do not have a lot of external dependencies, such as NuGet, or native libraries. The largest benefit of this method is that your migrated app will be in a single project, targeting all the .NET MAUI-supported platforms. If your original app only targeted Android and iOS, using this method could get you Mac Catalyst and Windows targets for free. Using .NET Upgrade Assistant will not add any platforms that you didn’t already target.

To illustrate these steps, we will create a new project, just like we did in Chapter 2. This time, however, we will have to create a Xamarin.Forms project first.

Creating a new Xamarin.Forms app

The following steps will guide you through creating a new Xamarin.Forms project:

  1. Open Visual Studio 2022 and select Create a new project:
Figure 3.1 – Visual Studio 2022

Figure 3.1 –...

Manual migration overview

In the previous section, we converted a simple Xamarin.Forms app into .NET MAUI using the Single Project system. This project did not use any advanced features of Xamarin.Forms, such as external NuGets, custom controls, or any commercial controls. These are additional items that you will have to consider when migrating your apps from Xamarin.Forms to .NET MAUI.

In this section, we are going to discuss the basic flow that you should go through to migrate your Xamarin.Forms app to .NET MAUI. This is by no means an all-inclusive list; the .NET MAUI team is updating a wiki page that details all their knowledge in one location.

Official migration guide

The guidelines for migrating your Xamarin.Forms apps to .NET MAUI are continually evolving since the release of .NET MAUI, based on usage and feedback. To review the latest guidelines, please visit the following URL in your favorite browser: https://learn.microsoft.com/en-us/dotnet/maui/get-started/migrate...

Installing and running .NET Upgrade Assistant

As stated previously, .NET Upgrade Assistant will attempt to perform the first four steps of the migration of your Xamarin.Forms app to .NET MAUI outlined in the previous section. The tool is under active development and as the team discovers new improvements, they are added. This is mostly due to feedback they receive from developers like you.

At the time of writing, .NET Upgrade Assistant did not work on all projects and has the following limitations:

  • Xamarin.Forms must be version 5.0 and higher
  • Only Android and iOS projects are converted
  • .NET MAUI must be properly installed with the appropriate workloads

If you have followed the steps from Chapter 1, then the last should should already be satisfied.

If your Xamarin.Forms app meets these criteria, then we can get started by installing the tool.

Installing .NET Upgrade Assistant

.NET Upgrade Assistant is a Visual Studio extension on Windows and a command...

Summary

In this chapter, we focused on upgrading a Xamarin.Forms app to .NET MAUI. We learned how to upgrade the project files from .NET Framework to SDK-style projects, application startup files, XAML views, and C# files needed for .NET MAUI. We started by doing this manually to learn about all the required steps and changes. We ended this chapter by using .NET Upgrade Assistant to make many of the changes for us. We also learned how to upgrade to the Single Project format, which is the default for .NET MAUI. Now, we can pick the best method for the project we are upgrading.

While we covered a lot in this chapter, it was not exhaustive. There is a lot of variation in different projects, from NuGet package dependencies and vendor-provided controls to customizations using renderers and effects. The app you are upgrading may use one or all of these, and you can find additional help on the Microsoft Learn site for upgrading Xamarin.Forms to .NET MAUI: https://learn.microsoft.com/en...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
.NET MAUI Projects - Third Edition
Published in: Feb 2024Publisher: PacktISBN-13: 9781837634910
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 $15.99/month. Cancel anytime

Authors (3)

author image
Michael Cummings

Michael Cummings is a Senior Development Engineer with Microsoft. He currently works on the Visual Studio tooling for building WPF, Xamarin.Forms, and .NET MAUI apps. He has experience as a developer and architect with concentrations in app development, design, deployment, and business process as it relates to technology. Michael has been a graphics and game programmer since the days of the TI99-4/A. He started the Boston XNA Developers Group. He has contributed to many open-source projects, including AXIOM, a .NET/Mono 3D rendering engine, and Planeshift, a 3D MMORPG. Michael also has experience with native game and graphics development (C++/DirectX) on Windows. Michael lives in Lexington, MA with his wife and their dog. When not working with technology he enjoys watching movies, trying out new recipes, and the occasional game of full-contact racquetball.
Read more about Michael Cummings

author image
Daniel Hindrikes

Daniel Hindrikes is a developer and architect with a passion for developing mobile apps powered by the cloud. Daniel fell in love with Xamarin in the early days of Xamarin when he realized that he could use C# even for iOS and Android apps, and that he could share code with the Windows apps he was also building. But Daniel started to build mobile apps long before that, working on Android apps with Java and even Java ME apps (a long, long time ago). Daniel enjoys sharing his knowledge and can be found speaking at conferences, blogging, or recording the podcast, The Code Behind. Daniel works at the company tretton37 in Sweden and has experience working with both local and global customers.
Read more about Daniel Hindrikes

author image
Johan Karlsson

Johan Karlsson has been working with Xamarin since the days of MonoTouch and Mono for Android, and it all started with writing a game. He is a full-stack developer, currently focusing on mobile applications using Xamarin, but has in the past worked a lot with ASP.NET MVC, Visual Basic.NET (not proud), and C# in general. Also, he's created a whole bunch of databases in SQL Server over the years. Johan works at tretton37 in Sweden and has about 20 years' experience in the trade of assembling ones and zeros.
Read more about Johan Karlsson