Overview
Platform‑specific APIs let you tailor your .NET MAUI app to each operating system’s native capabilities. Use #if
directives, partial classes, or the Microsoft.Maui.Controls.PlatformConfiguration
namespace to access platform‑specific features such as status bar color, native controls, and hardware sensors.
Getting Started
To begin, add the appropriate NuGet packages and reference the platform‑specific namespaces. The following snippet shows how to change the Android status bar color.
// MainPage.xaml.cs
using Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific.AppCompat;
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
this.On().SetStatusBarColor(Colors.DarkBlue);
}
}
Platform Specific Guides
- Android – Custom Renderers & Essentials
- iOS – UIAppearance & Permissions
- Windows – WinUI Interop
- macOS – Native Views
- Tizen – Sensors & Wearables
Common Patterns
Dependency Service
// Interface in shared project
public interface IDeviceInfo
{
string GetModel();
}
// Android implementation
[assembly: Dependency(typeof(DeviceInfo_Android))]
namespace MyApp.Droid
{
public class DeviceInfo_Android : IDeviceInfo
{
public string GetModel() => Android.OS.Build.Model;
}
}
// Usage
var info = DependencyService.Get<IDeviceInfo>();
string model = info?.GetModel() ?? "Unknown";
Partial Classes
Create a partial class in each platform project to encapsulate native behavior.
// Shared MainPage.xaml.cs
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
ApplyPlatformSpecifics();
}
partial void ApplyPlatformSpecifics();
}
// Android/MainPage.Android.cs
public partial class MainPage
{
partial void ApplyPlatformSpecifics()
{
// Android‑specific code
}
}