Fer
Fer

Reputation: 1995

How to migrate from Xamarin Forms to .NET MAUI a library that uses Xamarin Essentials SecureStorage

I have an app written in Xamarin Forms that I want to migrate to .NET MAUI.

This app references a library project I have written that uses Xamarin Essentials SecureStorage. Therefore, this project imports the NuGet Xamarin.Essentials.

On the other hand, I have followed one of the .NET MAUI tutorials to get a .NET MAUI app working: https://learn.microsoft.com/en-us/dotnet/maui/tutorials/notes-mvvm/?view=net-maui-7.0

The problem arises when I try to add my library project to the MAUI app. The app is compiled and apparently runs fine with this library, but when it reaches the code that uses the SecureStorate, it throws this exception and crashes:

Xamarin.Essentials.NotImplementedInReferenceAssemblyException

This functionality is not implemented in the portable version of this assembly. You should reference the NuGet package from your main application project in order to reference the platform-specific implementation.

System.TypeInitializationException: The type initializer for 'Xamarin.Essentials.SecureStorage' threw an exception. ---> Xamarin.Essentials.NotImplementedInReferenceAssemblyException: This functionality is not implemented in the portable version of this assembly. You should reference the NuGet package from your main application project in order to reference the platform-specific implementation.
  at at Xamarin.Essentials.AppInfo.PlatformGetPackageName()
  at at Xamarin.Essentials.AppInfo.get_PackageName()
  at at Xamarin.Essentials.SecureStorage..cctor()

The code that throws the exception is something like this:

var json = await SecureStorage.GetAsync("exampleKey");

I know SecureStorage is already available in .NET MAUI projects as they include the Microsoft.Maui.Essentials framework, so I could use SecureStorage in the code of this main project. But I want to make use of SecureStorage in my library project. It would be great if I could add this framework to my library, but don't know if it is a possibility.

I have tried adding Xamarin.Essentials NuGet to the main app project, althought I think it's not the best idea, but the exception still happens.

How could I proceed?

Edit 1: as @Gerald suggests I have added UseMauiEssentials flag to the library project configuration, but no luck with this. I get a compilation error when using SecureStorage methods. Followed these steps:

In library project .csproj:

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <PackOnBuild>true</PackOnBuild>
    <PackageVersion>1.1.0</PackageVersion>
    <Authors>Pepito</Authors>
    <Description>Mobility Serviceslibrary</Description>
    <PackageId>Mobility.Services</PackageId>
    <UseMauiEssentials>true</UseMauiEssentials>
  </PropertyGroup>

Upvotes: 0

Views: 1552

Answers (2)

Fer
Fer

Reputation: 1995

Finally I got it working. I had to create a new .NET MAUI Class Library that contained all the necessary configuration in order to make use of SecureStorage (and other Xamarin.Essentials items).

The steps I followed were:

  1. Add a new project to my Maui app of type .NET MAUI Class Library.
  2. Add to this project all the NuGets that my library had in Xamarin Forms, except Xamarin.Essentials.
  3. Add to this project all the classes my library had.
  4. Remove all references to Xamarin.Essentials from these clases.

Note: I tried to replace in the .csproj of the new project library the flag "UseMaui" by "UseMauiEssentials", but doing so the reference to SecureStorage cannot be found.

Upvotes: 1

Gerald Versluis
Gerald Versluis

Reputation: 34103

In your library project csproj file include this

<PropertyGroup>
  <UseMauiEssentials>true</UseMauiEssentials>
</PropertyGroup>

That will bring in just the .NET MAUI Essentials APIs without the rest of .NET MAUI

Upvotes: 1

Related Questions