dotnet platform_error ai_generated true

System.PlatformNotSupportedException: Operation is not supported on this platform. (Android)

ID: dotnet/maui-platform-not-supported-android

Also available as: JSON · Markdown · 中文
85%Fix Rate
82%Confidence
1Evidence
2024-05-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
.NET MAUI 6.0 active
.NET MAUI 7.0 active
.NET MAUI 8.0 active
Xamarin.Android 12.0 active
Xamarin.Android 13.0 active

Root Cause

A .NET MAUI or Xamarin.Forms app is calling an API that is not available on the Android platform, such as Windows-specific filesystem APIs or iOS-only frameworks, without proper platform checks.

generic

中文

.NET MAUI 或 Xamarin.Forms 应用程序调用了 Android 平台不可用的 API,例如 Windows 特定的文件系统 API 或 iOS 专属框架,而没有进行适当的平台检查。

Official Documentation

https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/

Workarounds

  1. 85% success Use conditional compilation with platform-specific implementations. Example: #if ANDROID // Android-specific code using Android.Content.Intent #elif IOS // iOS-specific code using UIKit.UIApplication #endif
    Use conditional compilation with platform-specific implementations. Example: #if ANDROID // Android-specific code using Android.Content.Intent #elif IOS // iOS-specific code using UIKit.UIApplication #endif
  2. 90% success Use the DeviceInfo.Platform property from Xamarin.Essentials or MAUI Essentials to check at runtime: if (DeviceInfo.Platform == DevicePlatform.Android) { /* Android code */ } else { /* fallback */ }
    Use the DeviceInfo.Platform property from Xamarin.Essentials or MAUI Essentials to check at runtime: if (DeviceInfo.Platform == DevicePlatform.Android) { /* Android code */ } else { /* fallback */ }
  3. 95% success Use dependency injection with platform-specific services. Register an interface in the shared project and implement it in each platform project. Example: services.AddSingleton<IFileService, AndroidFileService>();
    Use dependency injection with platform-specific services. Register an interface in the shared project and implement it in each platform project. Example: services.AddSingleton<IFileService, AndroidFileService>();

中文步骤

  1. Use conditional compilation with platform-specific implementations. Example: #if ANDROID // Android-specific code using Android.Content.Intent #elif IOS // iOS-specific code using UIKit.UIApplication #endif
  2. Use the DeviceInfo.Platform property from Xamarin.Essentials or MAUI Essentials to check at runtime: if (DeviceInfo.Platform == DevicePlatform.Android) { /* Android code */ } else { /* fallback */ }
  3. Use dependency injection with platform-specific services. Register an interface in the shared project and implement it in each platform project. Example: services.AddSingleton<IFileService, AndroidFileService>();

Dead Ends

Common approaches that don't work:

  1. Adding a try-catch around the call and ignoring the exception. 90% fail

    This suppresses the error but does not provide the intended functionality; it may lead to silent data loss or incorrect behavior.

  2. Using conditional compilation (#if ANDROID) without implementing an alternative for Android. 80% fail

    If the alternative is not implemented, the code will not execute at all, leaving functionality missing.

  3. Downgrading the target framework to an older version that supports the API. 100% fail

    The API is platform-specific; downgrading does not change the underlying OS capabilities.