maui-platform-invoke

Coordinate native APIs in .NET MAUI via partial classes and DI.

Updated Nov 27, 2025
One-click install
npx skills add https://github.com/seydakaratekeli/KamPay3 --skill maui-platform-invoke-seydakaratekeli
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: maui-platform-invoke
Source: https://github.com/seydakaratekeli/KamPay3/tree/main/KamPay/.github/skills/maui-platform-invoke%20-%20Kopyala
Command: npx skills add https://github.com/seydakaratekeli/KamPay3 --skill maui-platform-invoke-seydakaratekeli

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

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.

Frequently Asked Questions about maui-platform-invoke

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I call platform-specific native APIs in .NET MAUI without creating brittle code?▼

Call platform-specific native APIs in .NET MAUI by using partial classes for platform implementations under Platforms/{Platform} and registering service interfaces via dependency injection. This approach ensures your code remains testable and avoids platform-locking.

What is the best way to organize multi-targeting and conditional compilation in MAUI projects?▼

The best way to organize multi-targeting in MAUI is using MSBuild compile conditions for custom file patterns like *.android.cs. Reserve small inline #if checks for minor fallbacks and ensure you provide #else blocks to prevent missing implementation errors.

How does dependency injection work for platform-specific services in MAUI?▼

Dependency injection for MAUI platform services works by defining a shared interface and registering it in MauiProgram.cs. You then provide per-platform implementations, enabling mocking and swapping during testing without directly referencing platform-specific APIs.

Can I use custom file naming patterns for multi-targeting in .NET MAUI?▼

Yes, you can use custom file naming patterns for multi-targeting in .NET MAUI by applying MSBuild compile conditions. This safely includes files like *.android.cs or *.ios.cs so they only compile on the intended target OS.

Why does my MAUI partial class platform code fail to compile across target frameworks?▼

MAUI partial class platform code fails to compile if the namespaces are inconsistent across shared and platform-specific files. Ensure all partial class files share the exact same namespace and verify your MSBuild multi-targeting conditions are correctly configured.

When should I use #if conditional compilation versus partial classes in MAUI?▼

Use #if conditional compilation for small inline checks requiring minor platform fallbacks, but prefer partial classes for substantial platform-specific logic. Partial classes keep platform code isolated in Platforms/{Platform} files, preventing brittle and untestable conditional spaghetti code.