Getting Started with .NET MAUI

Welcome to .NET MAUI (Multi-platform App UI), the evolution of Xamarin.Forms! .NET MAUI is a cross-platform framework for creating native mobile and desktop applications with C# and XAML from a single shared codebase.

What is .NET MAUI?

.NET MAUI allows you to build applications for Android, iOS, macOS, and Windows using a unified API. It leverages the power of .NET 6+ to deliver high-performance, modern applications with a rich UI experience.

Prerequisites

Before you begin, ensure you have the following installed:

You can download the .NET SDK from the official .NET website.

Setting Up Your Development Environment

Follow these steps to set up your environment:

1

Install Visual Studio 2022

Download and install Visual Studio 2022. During the installation, select the ".NET Multi-platform App UI development" workload. This will install all the necessary components for MAUI development.

If you already have Visual Studio 2022 installed, you can modify your installation by going to the Visual Studio Installer, clicking "Modify", and selecting the ".NET Multi-platform App UI development" workload.

2

Install .NET MAUI Workload (if not using Visual Studio)

If you prefer to use the command line or another IDE, you can install the .NET MAUI workload using the .NET CLI:

dotnet workload install maui

This command will download and install the necessary components for MAUI development.

3

Configure Target Platforms

Ensure you have the SDKs for your target platforms installed:

  • Android: The Visual Studio workload includes the Android SDK. You may need to download specific Android SDK components via the Android SDK Manager in Visual Studio.
  • iOS: For iOS development, you'll need Xcode installed on a Mac. Ensure your Mac is configured for development with Visual Studio.
  • macOS: Xcode is required for macOS app development.
  • Windows: The Windows App SDK is typically included with the Visual Studio workload.

Creating Your First .NET MAUI App

Let's create a simple "Hello, World!" application.

1

Using Visual Studio

1. Open Visual Studio 2022.

2. Click "Create a new project".

3. In the project templates search bar, type ".NET MAUI" and select the ".NET MAUI App" template.

4. Click "Next".

5. Configure your project: enter a project name (e.g., "MyMauiApp") and a location. Click "Next".

6. Select the .NET framework version (e.g., .NET 6.0). Click "Create".

2

Using the .NET CLI

Open your terminal or command prompt and run the following commands:

dotnet new maui -n MyMauiApp
cd MyMauiApp

This will create a new MAUI project named "MyMauiApp" in the current directory.

Running Your App

Once your project is created, you can run it on an emulator or a physical device.

1

Running from Visual Studio

1. Select your target platform (e.g., Android Emulator, Windows Machine) from the device dropdown in the Visual Studio toolbar.

2. Click the "Run" button (green triangle).

Visual Studio will build your app and deploy it to the selected target.

2

Running from the .NET CLI

To run your app from the .NET CLI, you'll need to specify the target framework and runtime identifier. For example, to run on Android:

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

For Windows:

dotnet build -t:Run -f net6.0-windows10.0.19041.0

Note: The exact runtime identifiers might vary slightly based on your installation.

Tip:

For iOS and macOS, running from the CLI typically involves additional setup and pairing with a Mac. Visual Studio for Mac or Visual Studio with Hot Restart (for iOS) is often more convenient for these platforms.

Exploring the Project Structure

The generated .NET MAUI project has a standard structure:

Next Steps

Congratulations on setting up .NET MAUI and creating your first app! From here, you can start exploring: