Platform-specific Initialization versus .NET MAUI Application Class

dg2k 1,386 Reputation points
2022-10-14T21:09:35.133+00:00

Under Xamarin, the Application class Xamarin.Forms.Application depends on the LoadApplication(new App) call that need to be executed during a platform-specific launch.

With .NET MAUI, the Application class Microsoft.Maui.Controls.Application now has a life of its own. It means that with .NET MAUI, the Application class can be executed before platform-specific initialization prerequisites are completed. Migration from Xamarin to .NET MAUI will therefore require to take care of this important difference.

An ad-hoc approach can be taken to solve the problem so as to sync platform-specific initialization with dependent code in the .NET MAUI Application class and subsequent user code. As important the .NET MAUI Application lifecycle is, I am assuming that there may be a solution that is robust out there, as opposed to an ad-hoc approach.

SCENARIO EXAMPLE:

I'm using Plugin.Fingerprint package for fingerprint sensor based security. The package requires initialization during platform launch, for instance, for Android CrossFingerprint.SetCurrentActivityResolver(() => this); initialization is required in MainActivity OnCreate().

The issue is that the fingerprint sensor code can be reached before the required platform initialization.

Is there a recommended approach to solving this issue?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,968 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,311 Reputation points Microsoft Vendor
    2022-10-17T06:17:09.417+00:00

    Hello,

    The issue is that the fingerprint sensor code can be reached before the required platform initialization.Is there a recommended approach to solving this issue?

    You can add your code of initialization to the MauiProgram.cs and wrap the Conditional compilation for Platform-specific code like the following code.

       public static class MauiProgram  
       {  
           public static MauiApp CreateMauiApp()  
           {  
               var builder = MauiApp.CreateBuilder();  
       #if ANDROID  
       // execute your android init code  
       #elif IOS  
       // execute your iOS init code  
       #else  
       // execute other platform init code  
         
       #endif  
         
               builder  
                   .UseMauiApp<App>()  
                   .ConfigureFonts(fonts =>  
                   {  
                       fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");  
                       fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");  
                   }) ;  
              return builder.Build();  
           }  
       }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful