The Essentials APIs
What used to be the separate Xamarin.Essentials package is now built directly into .NET MAUI as the Microsoft.Maui.Devices, Microsoft.Maui.Storage, Microsoft.Maui.Media, and Microsoft.Maui.Sensors namespaces, giving shared C# code a single cross-platform API surface for things like geolocation, the camera, the accelerometer, and battery status. Under the hood each API is backed by a platform-specific implementation, but the calling code — for example await Geolocation.Default.GetLocationAsync() — is identical whether running on Android, iOS, or Windows.
Cricket analogy: This is like the DRS (Decision Review System) presenting the same on-screen interface to broadcasters worldwide, while the underlying Hawk-Eye and Snicko hardware differs by stadium and vendor behind the scenes.
Geolocation and Sensors
Geolocation.Default.GetLocationAsync(GeolocationRequest) returns a single Location reading with configurable accuracy and timeout, while Geolocation.Default.GetLastKnownLocationAsync() returns a cached position instantly without triggering the GPS hardware, useful when you need a rough starting point before a precise fix arrives. Sensors like Accelerometer, Gyroscope, and Compass follow a subscribe/event pattern instead — call Start(SensorSpeed) and handle the ReadingChanged event, then always call Stop() when you're done to avoid draining battery.
Cricket analogy: GetLastKnownLocationAsync is like consulting yesterday's pitch report for a quick read before the toss, while GetLocationAsync is like waiting for the ground staff's fresh, precise pitch inspection just before play starts.
// One-shot precise location
var request = new GeolocationRequest(GeolocationAccuracy.Medium, TimeSpan.FromSeconds(10));
Location location = await Geolocation.Default.GetLocationAsync(request);
// Continuous accelerometer readings
Accelerometer.Default.ReadingChanged += (s, e) =>
{
var data = e.Reading;
Console.WriteLine($"X:{data.Acceleration.X} Y:{data.Acceleration.Y} Z:{data.Acceleration.Z}");
};
Accelerometer.Default.Start(SensorSpeed.UI);
// later, when the page disappears
Accelerometer.Default.Stop();Camera, Media, and Connectivity
MediaPicker.Default.CapturePhotoAsync() launches the native camera UI and returns a FileResult you can stream into a Stream or save locally, while MediaPicker.Default.PickPhotoAsync() opens the gallery picker instead — both return null if the user cancels rather than throwing. Connectivity.Current.NetworkAccess reports whether the device currently has internet access, and subscribing to Connectivity.ConnectivityChanged lets the app react live to Wi-Fi drops or switching to cellular data.
Cricket analogy: CapturePhotoAsync is like calling for a fresh third-umpire camera angle right now, while PickPhotoAsync is like pulling an existing replay clip from the broadcast archive instead of shooting new footage.
Every Essentials API that touches hardware — Geolocation, MediaPicker, Sensors — requires the matching permission to be granted first (see the Permissions and Lifecycle topic); calling these APIs without checking permission first throws a PermissionException on most platforms rather than silently failing.
Forgetting to call Stop() on a sensor like Accelerometer or Gyroscope keeps it actively sampling in the background even after the page has been navigated away from, which drains battery and can throw if the subscribing page's handler references disposed UI elements — always unsubscribe and Stop() in OnDisappearing.
- Device APIs live in Microsoft.Maui.Devices, .Storage, .Media, and .Sensors, unified from the old Xamarin.Essentials package.
- Geolocation.GetLocationAsync gets a fresh fix; GetLastKnownLocationAsync returns a cached position instantly.
- Sensors use a Start/Stop plus ReadingChanged event subscription pattern rather than a single async call.
- MediaPicker.CapturePhotoAsync opens the camera; PickPhotoAsync opens the gallery, both returning null on cancel.
- Connectivity.Current.NetworkAccess and ConnectivityChanged let the app detect and react to network state.
- Hardware APIs require the matching runtime permission or they throw a PermissionException.
- Always Stop() sensors in OnDisappearing to avoid battery drain and stale-handler crashes.
Practice what you learned
1. What is the key difference between GetLocationAsync and GetLastKnownLocationAsync?
2. How do you consume accelerometer data in .NET MAUI?
3. What happens when a user cancels the camera during MediaPicker.CapturePhotoAsync()?
4. What typically happens if you call a hardware API like Geolocation without first checking/requesting the matching permission?
Was this page helpful?
You May Also Like
Permissions and Lifecycle
Understand runtime permission requests and the app lifecycle events that govern how a .NET MAUI app behaves in the foreground and background.
Platform-Specific Code
Learn how to branch behavior per platform in .NET MAUI using conditional compilation, partial classes, and platform folders.
Pages and Modals
Understand the ContentPage lifecycle and how modal navigation differs from Shell's normal push navigation in .NET MAUI.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics