In .NET MAUI, you can write platform-specific code using several approaches:
Using
#if
compiler directivesUsing Dependency Injection (DI) with platform implementations
Using partial classes and platform folders
Here's a complete example using all three approaches for a feature that displays the current device's platform name.
Approach 1: Using Compiler Directives (#if
)
public string GetPlatformName() { string platform; #if ANDROID platform = "Android"; #elif IOS platform = "iOS"; #elif MACCATALYST platform = "Mac Catalyst"; #elif WINDOWS platform = "Windows"; #else platform = "Unknown"; #endif return platform; }
Approach 2: Using Dependency Injection (Recommended)
1. Create an Interface
public interface IDeviceService { string GetPlatformName(); }
2. Implement Platform-Specific Services
Android Implementation (Platforms/Android/DeviceService.android.cs):
using YourApp.Platforms.Android; namespace YourApp.Services { public class DeviceService : IDeviceService { public string GetPlatformName() => "Android"; } }
iOS Implementation (Platforms/iOS/DeviceService.ios.cs):
using YourApp.Platforms.iOS; namespace YourApp.Services { public class DeviceService : IDeviceService { public string GetPlatformName() => "iOS"; } }
3. Register the Service in MauiProgram.cs
public static class MauiProgram { public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); builder .UseMauiApp<App>() .ConfigureFonts(fonts => { fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); }); #if ANDROID builder.Services.AddSingleton<IDeviceService, Platforms.Android.DeviceService>(); #elif IOS builder.Services.AddSingleton<IDeviceService, Platforms.iOS.DeviceService>(); #endif return builder.Build(); } }
4. Use the Service in Your Code
public partial class MainPage : ContentPage { private readonly IDeviceService _deviceService; public MainPage(IDeviceService deviceService) { InitializeComponent(); _deviceService = deviceService; var platformName = _deviceService.GetPlatformName(); PlatformLabel.Text = $"Running on: {platformName}"; } }
Approach 3: Using Partial Classes and Platform Folders
1. Create a Partial Class in Shared Project
public partial class DeviceInfo { public partial string GetPlatformName(); }
2. Implement Platform-Specific Parts
Android Implementation (Platforms/Android/DeviceInfo.android.cs):
public partial class DeviceInfo { public partial string GetPlatformName() => "Android"; }
iOS Implementation (Platforms/iOS/DeviceInfo.ios.cs):
public partial class DeviceInfo { public partial string GetPlatformName() => "iOS"; }
3. Use the Class
var deviceInfo = new DeviceInfo(); var platform = deviceInfo.GetPlatformName();
Complete Example with Dependency Injection (Recommended)
Here's a complete end-to-end example using the DI approach:
Create a new MAUI project
Add IPlatformInfoService.cs:
public interface IPlatformInfoService { string GetPlatformName(); string GetPlatformVersion(); }
Add Android implementation (Platforms/Android/PlatformInfoService.cs):
using Android.OS; public class PlatformInfoService : IPlatformInfoService { public string GetPlatformName() => "Android"; public string GetPlatformVersion() { return Build.VERSION.Release; } }
Add iOS implementation (Platforms/iOS/PlatformInfoService.cs):
using UIKit; public class PlatformInfoService : IPlatformInfoService { public string GetPlatformName() => "iOS"; public string GetPlatformVersion() { return UIDevice.CurrentDevice.SystemVersion; } }
Register services in MauiProgram.cs:
var builder = MauiApp.CreateBuilder(); builder .UseMauiApp<App>() .ConfigureFonts(fonts => { /* font config */ }); // Register platform-specific services #if ANDROID builder.Services.AddSingleton<IPlatformInfoService, Platforms.Android.PlatformInfoService>(); #elif IOS builder.Services.AddSingleton<IPlatformInfoService, Platforms.iOS.PlatformInfoService>(); #endif // Register MainPage with dependency builder.Services.AddTransient<MainPage>(); return builder.Build();
Use in MainPage.xaml.cs:
public partial class MainPage : ContentPage { public MainPage(IPlatformInfoService platformInfo) { InitializeComponent(); PlatformLabel.Text = $"Running on: {platformInfo.GetPlatformName()}"; VersionLabel.Text = $"Version: {platformInfo.GetPlatformVersion()}"; } }
Update MainPage.xaml:
<VerticalStackLayout> <Label x:Name="PlatformLabel" FontSize="24" HorizontalOptions="Center"/> <Label x:Name="VersionLabel" FontSize="18" HorizontalOptions="Center"/> </VerticalStackLayout>
This example shows the recommended way to handle platform-specific code in MAUI using dependency injection, which makes your code more testable and maintainable. The compiler will automatically include only the appropriate platform implementation when building for each target.
No comments:
Post a Comment