# System.PlatformNotSupportedException: 此平台不支持该操作。(Android)

- **ID:** `dotnet/maui-platform-not-supported-android`
- **领域:** dotnet
- **类别:** platform_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| .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 | — | — |

## 解决方案

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>();
   ```

## 无效尝试

- **Adding a try-catch around the call and ignoring the exception.** — This suppresses the error but does not provide the intended functionality; it may lead to silent data loss or incorrect behavior. (90% 失败率)
- **Using conditional compilation (#if ANDROID) without implementing an alternative for Android.** — If the alternative is not implemented, the code will not execute at all, leaving functionality missing. (80% 失败率)
- **Downgrading the target framework to an older version that supports the API.** — The API is platform-specific; downgrading does not change the underlying OS capabilities. (100% 失败率)
