Advertisement
Advanced .NET MAUI, Platform Integration & Handlers

How do you invoke platform APIs that have no MAUI abstraction, cleanly?

Short answer

Define a shared interface, implement it per platform (Platforms folder partial classes), and register in DI. This keeps native calls isolated and testable via the interface, unlike scattering #if throughout business logic.

Save .NET MAUI mock interview
Advertisement
AModel answer
Define a shared interface, implement it per platform (Platforms folder partial classes), and register in DI. This keeps native calls isolated and testable via the interface, unlike scattering #if throughout business logic. It's the recommended pattern for features like biometric auth or platform-specific storage.
public interface IBiometricAuth { Task<bool> AuthenticateAsync(string reason); }
// Platforms/iOS/BiometricAuth.cs -> LAContext
// Platforms/Android/BiometricAuth.cs -> BiometricPrompt
builder.Services.AddSingleton<IBiometricAuth, BiometricAuth>();
Real-world example
Face ID and Android BiometricPrompt sit behind one IBiometricAuth so the login view model calls a single method.
Likely follow-ups
  • Why an interface over #if? | How does this help testing?
Related topics