What problem does it solve?
Provides clear guidance and best practices for calling platform-specific native APIs from .NET MAUI apps so developers can implement Android, iOS, Mac Catalyst, and Windows functionality without creating untestable, brittle, or platform-locked code. It helps avoid common pitfalls like overusing #if blocks, mismatched partial class namespaces, missing fallbacks, and improper DI that prevents mocking and testing.
Core Features & Use Cases
- Conditional compilation guidance: When to use small inline #if checks versus other patterns.
- Partial classes for platform implementations: Organize platform-specific logic into Platforms/{Platform} files that compile only on the target OS.
- Multi-targeting & MSBuild patterns: How to include custom file name patterns (for example *.android.cs) safely with MSBuild conditions.
- Dependency injection & testability: Prefer interfaces and DI registration in MauiProgram.cs to enable mocking and swapping implementations.
- Practical pitfalls and checklists: Null-check Platform.CurrentActivity on Android, ensure #else fallbacks, and keep namespaces consistent across partial class files.
- Use Case Example: Implement a DeviceOrientationService with shared API and per-platform implementations to read device orientation reliably across platforms.
Quick Start
Implement a shared partial service in your project, add per-platform partial implementations under Platforms/{Platform}, register the service interface in MauiProgram.cs, and include MSBuild conditions for any custom file naming patterns.