MAUI Essentials

This section covers the fundamental building blocks and core concepts of .NET MAUI (Multi-platform App UI). Understanding these essentials will equip you to build robust and performant cross-platform applications.

What are MAUI Essentials?

.NET MAUI Essentials is a .NET Standard library that simplifies common cross-platform application development tasks. It provides APIs to access device capabilities and platform-specific features in a unified way, allowing you to write code once and run it on multiple platforms.

Key Features and Concepts:

Commonly Used Essentials APIs:

API Examples

Connectivity

Check the device's network connectivity status.

Essentials.Connectivity.NetworkAccess

Returns: NetworkAccess enum (e.g., Internet, WiFi, None)

Example Usage:

if (Connectivity.NetworkAccess == NetworkAccess.Internet)
{
    // Device is connected to the internet
    Console.WriteLine("Connected!");
}

Geolocation

Get the current location of the device.

await Geolocation.GetLocationAsync()

Returns: A Location object containing latitude and longitude.

Example Usage:

var location = await Geolocation.GetLocationAsync(new GeolocationRequest(GeolocationAccuracy.High));
if (location != null)
{
    Console.WriteLine($"Latitude: {location.Latitude}, Longitude: {location.Longitude}");
}

Preferences

Store and retrieve simple key-value data.

Preferences.Set("user_name", "John Doe"); string userName = Preferences.Get("user_name", "Guest");

Example Usage:

// Saving a string
Preferences.Set("last_app_version", "1.0.0");

// Retrieving a string with a default value
string version = Preferences.Get("last_app_version", "unknown");

// Saving a boolean
Preferences.Set("is_dark_mode", true);

// Retrieving a boolean
bool darkMode = Preferences.Get("is_dark_mode", false);

SecureStorage

Store sensitive data securely.

await SecureStorage.SetAsync("auth_token", "your_token_here"); string token = await SecureStorage.GetAsync("auth_token");

Example Usage:

// Store sensitive data
await SecureStorage.SetAsync("api_key", "super_secret_key_12345");

// Retrieve sensitive data
string apiKey = await SecureStorage.GetAsync("api_key");

// Remove sensitive data
SecureStorage.Remove("api_key");

Getting Started with Essentials

To use .NET MAUI Essentials in your project, ensure you have the necessary NuGet package installed. Most .NET MAUI projects include it by default. You can then access the APIs directly within your application code.

Refer to the official .NET MAUI Essentials documentation for a comprehensive list of APIs and detailed guides.