admin管理员组

文章数量:1305304

Getting Exception in MAUI app while trying to implement biometric on maui android using plugin.Fingerprint

the exception says: {System.InvalidOperationException: Resolver for the current activity is not set. Call Fingerprint.SetCurrentActivityResolver somewhere in your startup code.

however this is my mainactivity.cs

namespace dummy
{
  [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density, Exported = true)]
  public class MainActivity : MauiAppCompatActivity
  {
    protected override void OnCreate(Bundle savedInstanceState)
    {
      base.OnCreate(savedInstanceState);
      CrossFingerprint.SetCurrentActivityResolver(() => this);
    }

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
    {
      base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
      Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
  }
}

and here is how i implemented the authentication:

public async Task biometric()
{
  try
  {
    var availability = await CrossFingerprint.Current.IsAvailableAsync();

    if (!availability)
    {
      //await MainThread.InvokeOnMainThreadAsync(async () =>
      //{
      //    await DisplayAlert("Warning!", "No biometrics available", "OK");
      //});
      return;
    }

    var authResult = await CrossFingerprint.Current.AuthenticateAsync(
         new AuthenticationRequestConfiguration(
                "Login to dummy", 
                "Unlock your device to use dummy."));

    if (authResult.Authenticated)
    {
      if (!string.IsNullOrEmpty(usrname) && !string.IsNullOrEmpty(pasword))
      {
        AutoLogin(usrname, pasword);
      }
      else
      {
        await DisplayAlert("Error", "Login details missing!", "OK");
      }
    }
  }
  catch (Exception ex)
  {
    Console.WriteLine(ex.Message);
  }
}

本文标签: