Upgrade from Xamarin Native to .NET MAUI Native
Published: 2024-04-10 14:25:56

**1.   Workloads Installation**
**2.   Migrating Class Library project.**
**3.   Migrating Android project**
**4.   Advantages of Migrating to .NET 8**
**5.   Troubleshooting the issues after migration.**

To facilitate the migration of a Xamarin Native app to .NET MAUI native, this article focuses on exploring the transition journey from Xamarin.Android to .NET MAUI Native. It outlines the migration process and highlights the benefits it brings. Thus, our aim is to follow the outlined procedure for a seamless transition.

---

**Workloads Installation**

1. Install .NET MAUI Related Components: Install the necessary components for .NET MAUI through the Visual Studio Installer. Here are the general steps:

- Open Visual Studio.
- Go to the "Extensions" menu and select "Manage Extensions."
- In the "Manage Extensions" window, search for ".NET MAUI" or "MAUI."
- Install the .NET MAUI workload.

---

**Migrating Class Library project**

The following modifications have been made in the class library project within our applications.

1. **Change Target Framework to .NET 7.0:** Adjust the project's target framework from .NET Standard 2.1 to .NET 
![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpz3ubb2fwjk11w6jsqt.png)
2. **Update Package Versions:** Update all package versions to those compatible with .NET 6.0 and above.
![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oatdicn3272uxye8dr76.png)
3. **Replace Xamarin.Essentials with Maui.Essentials:** Remove Xamarin.Essentials and integrate Maui.Essentials by enabling UseMauiEssentials in both Android and Core .csproj files.
![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aaguqlxcfo3tshywzghf.png)
4. **Fix Namespaces:** After the framework target change and replacement of Xamarin.Essentials, address namespace issues using the "Find and Replace" feature in your code editor.
5. **Build and Test:** Rebuild the project and verify that there are no build errors.

---

**Migrating Android project**
To streamline the migration process from Xamarin.Native to .NET MAUI, it's advisable to initiate a new project using the .NET framework and then migrate the existing files into this new project. This approach simplifies the migration for all platforms. Updating packages targeting .NET 6 and above is necessary.

1. **Add a Separate Android MAUI Project:** Create a new Android MAUI project within your solution.
  - Right-click on your solution in the Solution Explorer.
  - Select "Add" > "New Project..."
  - In the "Create a new project" dialog, search for "MAUI Android App."
  - Follow the wizard to create the Android project.

2. **Copy Old Project Manifest Content:** Transfer relevant content such as AndroidManifest.xml, resources, and source code files from the old project to the new MAUI Android project, updating references and configurations as necessary.

3. **Create our own Application File:** Generate a new application file with the app name (i.e MSRApplication).
![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h1kwssz8a8t9kk8vb455.png)

4. **Copy and Update Packages:** Review and update package versions used in the old Android project to ensure compatibility with .NET 6.0 and above.

5. **AndroidX Migration:** Utilize AndroidX packages and replace any references to support v4 and v7 packages with AndroidX equivalents.

6. **Copy Old Project Files and Update Namespaces:** Copy relevant files and folders from the old Android project to the new MAUI Android project, updating namespaces to match the new MAUI Android project's namespace (e.g., ProjectName.Android).

7. **Build and Resolve Issues:** Address build issues such as deprecated APIs, package conflicts, and compatibility problems based on error messages and suggestions provided by Visual Studio.

8. **Testing and Debugging:** Thoroughly test the application on Android devices and emulators, debugging and fixing any runtime issues encountered during testing.

---

**Advantages of Migrating to .NET 7**
Migrating from Xamarin Native to .NET 7 offers several advantages, as .NET 7 (or later versions) provides improved features, performance enhancements, and better integration with modern development practices. Here are some advantages of migrating to .NET 7 from Xamarin Native:

1. **Reduced App Size:** After migrating, the app size decreases by approximately 30%.

2. **Access to New Language Features:** With .NET 7, developers gain access to new language features, enhancing code capabilities.

3. **Support for Latest Android API:** The migration .NET 8 enables support for Android API 34 (Android 14)

4. **Unified Platform:** With .NET 7, Microsoft has been working on unifying the .NET ecosystem, which includes Xamarin, ASP.NET, and other frameworks under one umbrella. This means better consistency and compatibility across different platforms, making it easier to share code between different projects.

5. **Performance Improvements:** .NET 7 comes with performance improvements across the board, including faster startup times, better runtime performance, and reduced memory usage. These improvements can lead to a smoother and more responsive user experience for your Xamarin app.

6. **Long-term Support:** .NET 7 and subsequent versions receive long-term support from Microsoft, ensuring that your Xamarin app remains compatible, secure, and stable over time. By migrating to .NET 7, you can benefit from ongoing updates and support from the .NET development team.

---

**Troubleshooting the issues after migration**

1. **Android:** Encountered issues with PdfViewer on Android API level 26 and below. Resolved by converting PDFs to images for devices below API level 26.

2. **Dependency Updates:** To utilize Mvx controls in Andorid API level 34 like MVXRecyclerView for .NET 8, it's necessary to use MVVMCross 9.1.3 or above.

3. **Transition to MAUI.Essentials:** In the migration process, had to switch to MAUI.Essentials from Xamarin.Essentials. We faced an issue to get the legacy storage, so we implemented a our own helper class to access legacy storage previously managed by Xamarin.Essentials this will prevent the app breakage during app upgrade.