Updating Old Xamarin Forms Projects

Recently, I had to update some old Xamarin Forms projects that had a few years without doing any package updates. It is important to mention that my task was just “Update all the packages to the latest version“, but as you probably know Xamarin has changed a lot in recent years.

Before updating the packages to the latest version, there are a few things you need to consider because if you don’t then you won’t be able to update most of them.

These are the main considerations:

  • Migrating to .NET Standard
  • Migrating the packages.config file
  • Removing components references
  • Libraries update
  • Android X migration

In this article, I’m going to guide you with the steps I followed and the main issues I faced while doing it.

Migrating to .NET Standard

(Before reading this part, if you are not familiar with .NET Standard I recommend you to check this post by Pierce Boggan in the Xamarin Blog about it ) .

These projects were PCL so the main step for me was to migrate them to .NET Standard. Why? Since most of the NuGet packages have already been updated to .NET Standard, they cannot be upgraded since PCL support has been removed.

You can easily know if your project is .NET Standard by looking at the project structure. A .NET Standard project should look like this:

Just migrate your PCL projects, no need to migrate your Android / iOS project.

Here the step by step:

1. Make a backup of your package.config file (That way you will know which packages you will have to install later).

2. Remove the packages.config file from your project.

3. Open the .csproj file of your project and remove everything there.

4. In your .csproj paste this snippet:

5. Add all your packages back.

In this step, you have two options: Adding them manually in the .csproj file or using the NuGet Package Manager.

After adding the packages your .csproj will look like this:

6. Remove the AssemblyInfo.file

7. Add deleted references

If your project depends on other projects make sure to add the reference back.

If you had resources such as JSON, TXT, RESX, etc… files. Make sure to add them back and set the build action as an EmbeddedResource.

8. Click on restore packages and rebuild your project

DONE! 🙂

Possible errors

After the migration I got these errors:

Fix it by deleting the bin/obj folders, then restore packages and rebuild it again. (If that doesn’t work, close and open Visual Studio).

Migrating your packages.config file in your Platform projects

In your platform projects Android/iOS if there’s a package.config file eventually you will have to move the packages to .csproj by using PackageReference tag.

I found this article on how to it in Windows by just clicking on Migrate package.config to PackageReference. But since I’m using Mac I had to it manually.

Here the step by step: 

1. Make a backup of your package.config file (that way you will know which packages you will have to install later)

2. Remove your packages.config file

3. Open your .csproj and remove all the old packages references

4. Add this tag to your .csproj <RestoreProjectStyle>PackageReference</RestoreProjectStyle>

5. Add your packages back again using PackageReference tag

After adding them your .csproj will look like this:

6. Click on restore packages and rebuild your project

Done 🙂

Migrating Components

If when opening your project you see this error message is because you have references to old Xamarin Components (Which were removed from Xamarin).

To fix it you just have to:

1. Open the .csproj file of each project, search for the XamarinComponentReference tag and remove it

2. Go to the project folder and remove the Components folder

3. Add the NuGet packages for those components or refactor the code

Since components no longer exist, most component creators migrated their libraries to NuGet packages, so you can probably find the equivalent NuGet for them. If it doesn’t exist, you can search for other alternatives or extract the component assemblies and reference them directly in your project.

You can find more info here.

Libraries Update

There may be packages that are no longer necessary, for example in my case they were:

  • Plugin.Permissions
  • Xam.Plugin.Connectivity
  • Xam.Plugin.Settings

Xamarin.Essentials now it has these built-in functionalities, so there is no need for those packages. So it’s time to refactor your code and replace these functionalities by using Essentials!

Android X Migration

“AndroidX is a major improvement to the original Android Support Library. AndroidX packages fully replace the Android Support Library by providing feature parity and new libraries.”

Lastest version of Xamarin.Forms and Xamarin.Essentials now support Android X, so to install them you will need to add Android X support.

Before doing the Android X migration I recommend you to do the “Migration of your packages.config file in your platform projects”.

Here the step by step: 

1. Open your Android project options and change your Compile using Android version: (Target Framework) to Android 10.

2. Open your Android Manifest file and select Target Android version to 10.0

3. In your Android project click on Migrate to Android X

4. Update all the migration packages installed

In my case, after clicking on Migrate to Android X it installed a pre-release version of the Android X Support packages, so I had to update all of them to the latest stable version.

5. Click on restore packages and rebuild your project

Done!

That’s all for now, hope it can help you to make your updating process easier.

Happy coding!

You may also like