MAUI APIs Reference

Explore the rich set of APIs available in .NET MAUI for building cross-platform applications.

Core UI Elements

This section covers the fundamental UI elements and controls that form the building blocks of your MAUI applications.

Microsoft.Maui.Controls Namespace

Provides access to the core UI controls, layouts, and properties.

Label Class

Displays a string of text.

Namespace: Microsoft.Maui.Controls

Inheritance: View > Element > BindableObject

Common Properties:
  • Text: The text content to display.
  • TextColor: The color of the text.
  • FontSize: The size of the font.
  • FontAttributes: Specifies font style (e.g., Bold, Italic).
  • HorizontalTextAlignment: Horizontal alignment of the text.
  • VerticalTextAlignment: Vertical alignment of the text.
Example:
var myLabel = new Label
                {
                    Text = "Hello, MAUI!",
                    TextColor = Colors.Blue,
                    FontSize = 24,
                    HorizontalTextAlignment = TextAlignment.Center
                };

Button Class

A control that can contain text or an image and that performs an action when clicked by the user.

Namespace: Microsoft.Maui.Controls

Common Properties:
  • Text: The text displayed on the button.
  • TextColor: The color of the button's text.
  • BackgroundColor: The background color of the button.
  • CornerRadius: The radius of the button's corners.
Events:
  • Clicked: Occurs when the button is clicked.
Example:
var myButton = new Button
                {
                    Text = "Click Me",
                    BackgroundColor = Colors.Green,
                    CornerRadius = 5
                };
                myButton.Clicked += (sender, e) => {
                    // Handle button click
                };

Entry Class

A control that accepts single-line text input.

Namespace: Microsoft.Maui.Controls

Common Properties:
  • Text: The current text in the entry.
  • Placeholder: Text displayed when the entry is empty.
  • Keyboard: Specifies the type of keyboard to display.
  • IsPassword: When true, masks the input.
Events:
  • TextChanged: Occurs when the text in the entry changes.
Example:
var nameEntry = new Entry
                {
                    Placeholder = "Enter your name",
                    Keyboard = Keyboard.Text
                };

Layouts

Arrange your UI elements effectively using MAUI's powerful layout containers.

Microsoft.Maui.Controls Namespace

StackLayout Class

Arranges child elements in a vertical or horizontal stack.

Namespace: Microsoft.Maui.Controls

Common Properties:
  • Orientation: Specifies whether to stack horizontally or vertically.
  • Spacing: The amount of space between elements.
Example:
var verticalStack = new StackLayout
                {
                    Orientation = StackOrientation.Vertical,
                    Spacing = 10,
                    Children = { new Label { Text = "Item 1" }, new Button { Text = "Item 2" } }
                };

Grid Class

Arranges child elements in a grid, defined by rows and columns.

Namespace: Microsoft.Maui.Controls

Common Properties:
  • RowDefinitions: Defines the rows in the grid.
  • ColumnDefinitions: Defines the columns in the grid.

To position an element, use Grid.Row and Grid.Column attached properties.

Example:
var myGrid = new Grid
                {
                    RowDefinitions = { new RowDefinition(), new RowDefinition() },
                    ColumnDefinitions = { new ColumnDefinition(), new ColumnDefinition() }
                };
                Grid.SetRow(myLabel, 0);
                Grid.SetColumn(myLabel, 0);
                myGrid.Children.Add(myLabel);

Platform Specific APIs

Access platform-specific features when needed.

Microsoft.Maui.Essentials Namespace

Provides access to platform-specific APIs like Geolocation, Sensors, and File System.

Geolocation Class

Provides APIs for accessing the device's location.

Namespace: Microsoft.Maui.Essentials

Methods:
  • GetLastKnownLocationAsync(): Gets the last known location.
  • GetLocationAsync(GeolocationRequest request): Gets the current location with specified accuracy and timeout.
Example:
var location = await Geolocation.GetLastKnownLocationAsync();
                if (location != null)
                {
                    Console.WriteLine($"Latitude: {location.Latitude}, Longitude: {location.Longitude}");
                }

DeviceInfo Class

Provides information about the device the app is running on.

Namespace: Microsoft.Maui.Essentials

Properties:
  • Model: The device model (e.g., "iPhone 13 Pro").
  • Manufacturer: The device manufacturer (e.g., "Apple").
  • Platform: The operating system platform (e.g., "iOS", "Android").
  • Idiom: The device idiom (e.g., Phone, Tablet, Desktop).
  • VersionString: The operating system version.
Example:
Console.WriteLine($"Device Model: {DeviceInfo.Model}");
                Console.WriteLine($"Platform: {DeviceInfo.Platform}");