.NET MAUI Development
Last updated: 2023-10-27
Welcome to the official Microsoft documentation for developing cross-platform applications with .NET Multi-platform App UI (.NET MAUI). .NET MAUI is the evolution of Xamarin.Forms, enabling you to build native mobile and desktop applications with a single C# codebase.
Key Features of .NET MAUI
- Single Project: Manage all your platform-specific code and resources in a single project.
- Native UI: Render native UI controls on each target platform, ensuring a familiar look and feel.
- Extensive API Access: Access platform-specific APIs and features directly from your C# code.
- Modern .NET: Built on the latest .NET technologies, offering performance improvements and new language features.
- Cross-Platform Targets: Develop for Android, iOS, macOS (via Mac Catalyst), and Windows (via WinUI 3).
Getting Started
Before you begin, ensure you have the necessary development tools installed.
- Install .NET SDK: Download and install the latest .NET SDK from the official .NET website.
- Install .NET MAUI Workload: Use the .NET CLI to install the MAUI workload:
dotnet workload install maui
- IDE Support: Visual Studio 2022 is the recommended IDE for .NET MAUI development. Make sure to select the ".NET Multi-platform App UI development" workload during installation.
Once your environment is set up, you can create your first .NET MAUI application. For detailed steps, refer to the Get Started guide.
Core Concepts
Understanding the fundamental concepts of .NET MAUI is crucial for effective development:
1. XAML for UI Definition
XAML (Extensible Application Markup Language) is the primary way to define your application's user interface. It allows for a declarative and readable way to structure your UI elements.
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyMauiApp.MainPage"
Title="Hello MAUI">
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<Image
Source="dotnet_bot.png"
SemanticProperties.Description="Cute dot net bot sitting on a rainbow"
HeightRequest="200"
HorizontalOptions="Center" />
<Label
Text="Hello, World!"
SemanticProperties.HeadingLevel="Level1"
FontSize="32"
HorizontalOptions="Center" />
<Label
Text="Welcome to .NET MAUI!"
SemanticProperties.HeadingLevel="Level2"
SemanticProperties.Tooltip="Informational element shown when having focus"
FontSize="18"
HorizontalOptions="Center" />
<Button
Text="Click me"
SemanticProperties.Hint="Counts the number of times you click"
Clicked="OnCounterClicked"
HorizontalOptions="Center" />
</VerticalStackLayout>
</ContentPage>
2. C# Code-Behind
While XAML defines the UI structure, C# code-behind files handle the logic, event handling, and data manipulation for your UI elements.
namespace MyMauiApp;
public partial class MainPage : ContentPage
{
int count = 0;
public MainPage()
{
InitializeComponent();
}
private void OnCounterClicked(object sender, EventArgs e)
{
count++;
CounterLabel.Text = $"Current count: {count}";
}
}
3. Data Binding
Data binding simplifies the synchronization of data between your UI elements and your application's data model. This reduces boilerplate code and makes your application more maintainable.
Explore the Data Binding in MAUI documentation for detailed examples and patterns.
4. .NET MAUI Essentials
.NET MAUI Essentials provides a unified API to access platform-specific functionality such as device sensors, storage, network connectivity, and more, abstracting away the platform differences.
Learn more about MAUI Essentials.
Further Resources
Developer Tip
Leverage the .NET MAUI Community Toolkit for pre-built controls, custom renderers, and helpful behaviors that can accelerate your development.