Invoke Platform Code in .NET MAUI
Xamarin.Forms used DependencyService to reach native functionality, but .NET MAUI can do better. David Britch demonstrates a cleaner approach built on partial classes, partial methods, and multi-targeting.
What you’ll learn
- How multi-targeting works — the Platforms folder and how the compiler includes only the current platform’s code
- Partial method pattern — declaring a partial method in cross-platform code and implementing it per platform
- A worked example — a
MyService.GetPlatformmethod with Android, iOS, macOS, and Windows implementations - Calling it — invoking the combined class from cross-platform code with no
DependencyServicerequired - Filename-based targeting — an alternative to Platforms folders using names like
MyService.Android.cs
Read the full post for the complete code and a link to the sample on GitHub.