.NET MAUI Documentation

Welcome to .NET MAUI

.NET Multi-platform App UI (.NET MAUI) is an open-source, cross-platform framework for creating native mobile and desktop applications with C# and XAML from a single shared codebase.

.NET MAUI is the evolution of Xamarin.Forms, providing a more streamlined and integrated development experience with .NET 6 and later.

With .NET MAUI, you can build apps for:

  • Android
  • iOS
  • macOS
  • Windows

Getting Started

Installation

To start developing with .NET MAUI, you need to install the .NET SDK. You can download the latest version from the official .NET website.

Once the .NET SDK is installed, you can install the MAUI workload using the .NET CLI:

dotnet workload install maui

Creating Your First App

You can create a new .NET MAUI project using the .NET CLI:

dotnet new maui -n MyMauiApp

Then, navigate into the project directory and run your app:

cd MyMauiApp
dotnet build -t:Run -f net6.0-android

Replace net6.0-android with your desired target framework (e.g., net6.0-ios, net6.0-maccatalyst, net6.0-windows).

Core Concepts

Xamarin.Forms vs. .NET MAUI

.NET MAUI is built upon the foundation of Xamarin.Forms, offering significant improvements:

  • Unified Project Structure: A single project for all target platforms.
  • Modern .NET: Leverages the latest .NET APIs and features.
  • Improved Performance: Optimized for faster startup and runtime performance.
  • Extensibility: Enhanced platform extensibility model.

Project Structure

A .NET MAUI project has a simplified structure compared to its predecessors. Key folders include:

  • Platforms: Contains platform-specific code and resources.
  • Resources: Holds shared assets like images, fonts, and styles.
  • App.xaml and App.xaml.cs: The application entry point.
  • AppShell.xaml and AppShell.xaml.cs: Defines the app's overall structure and navigation.

XAML Basics

XAML (Extensible Application Markup Language) is used for defining your user interface declaratively. It allows you to separate the UI design from the application logic.

Simple XAML Example

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MyMauiApp.MainPage"> <VerticalStackLayout Spacing="25" Padding="30,0" VerticalOptions="Center"> <Image Source="dotnet_bot.png" SemanticProperties.Hint="Cute dot net bot mascot" HeightRequest="200" HorizontalOptions="Center" /> <Label Text="Hello, .NET MAUI!" SemanticProperties.HeadingLevel="Level1" FontSize="32" HorizontalOptions="Center" /> <Button x:Name="CounterBtn" Text="Click me" SemanticProperties.Hint="Counts the number of times you click" Clicked="OnCounterClicked" HorizontalOptions="Center" /> </VerticalStackLayout> </ContentPage>

Data Binding

Data binding is a powerful mechanism that connects UI elements to data sources, enabling dynamic updates and simplifying code-behind logic.

You can bind properties of UI elements to properties of your data model using XAML:

<Label Text="{Binding YourModelProperty}" />

MVVM Pattern

The Model-View-ViewModel (MVVM) architectural pattern is highly recommended for .NET MAUI development. It promotes separation of concerns and testability:

  • Model: Represents your application's data and business logic.
  • View: The user interface, typically defined in XAML.
  • ViewModel: Acts as an intermediary between the View and the Model, exposing data and commands for the View.

UI Development

Layouts

.NET MAUI provides various layout containers to arrange your UI elements:

  • VerticalStackLayout: Arranges elements in a vertical column.
  • HorizontalStackLayout: Arranges elements in a horizontal row.
  • Grid: Arranges elements in rows and columns.
  • FlexLayout: A flexible layout that can align and distribute items.
  • ScrollView: Allows content to be scrollable.

Controls

A rich set of controls are available for building your UI, including:

  • Buttons, Labels, Entry fields
  • Images, Icons
  • Lists (CollectionView)
  • Navigation elements (TabbedPage, Shell)
  • And many more...

Styling and Theming

You can style your application using:

  • Resource Dictionaries: Define styles, colors, and templates centrally.
  • Implicit Styles: Apply styles to all instances of a specific control type.
  • Explicit Styles: Apply styles to specific controls by referencing their keys.
  • Themes: Create different visual themes for your app.

.NET MAUI supports several navigation patterns:

  • Modal Navigation: Presenting pages modally.
  • Hierarchical Navigation: Pushing and popping pages onto/off a navigation stack.
  • Tabbed Navigation: Using a tabbed interface.
  • Shell Navigation: A simplified navigation model provided by .NET MAUI Shell.

Accessing Platform APIs

.NET MAUI provides abstractions for accessing native platform features.

Device Information

Get information about the device, such as manufacturer, model, and operating system version.

var deviceInfo = DeviceInfo.Current;
Console.WriteLine($"Running on: {deviceInfo.Manufacturer} {deviceInfo.Model}");

Sensors

Access device sensors like accelerometer, compass, and gyroscope.

if (Accelerometer.Default.IsSupported)
{
    var reading = await Accelerometer.Default.Reading();
    Console.WriteLine($"Acceleration: X={reading.X}, Y={reading.Y}, Z={reading.Z}");
}

File System Access

Read and write files to the device's local storage.

var path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var filePath = Path.Combine(path, "mydata.txt");

File.WriteAllText(filePath, "This is some data.");
var content = File.ReadAllText(filePath);
Console.WriteLine(content);
Note: For some platform-specific features, you might need to use platform-specific code via dependency injection or interface implementations.

Deployment

Deploy your .NET MAUI applications to various app stores:

  • Android: Package as an APK or App Bundle for the Google Play Store.
  • iOS: Archive your app for distribution through the App Store.
  • macOS: Package as a .app bundle for the Mac App Store or direct distribution.
  • Windows: Package as an MSIX for the Microsoft Store or side-loading.

Refer to the official documentation for detailed deployment guides for each platform.

Community & Support

Join the .NET MAUI community to get help, share your work, and contribute to the project.