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:
- .NET 6 SDK or later.
- Visual Studio 2022 (version 17.3 or later) with the ".NET Multi-platform App UI development" workload installed. This includes the necessary SDKs and tools for Android, iOS, and macOS development.
- For macOS development, you'll need Xcode.
You can download the .NET SDK from the official .NET website.
Setting Up Your Development Environment
Follow these steps to set up your environment:
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.
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.
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.
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".
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.
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.
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.
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:
- Platforms: Contains platform-specific code and resources.
- Resources: Holds fonts, images, and the
App.xaml
file for app-level resources. - App.xaml / App.xaml.cs: The application entry point and resource dictionary.
- AppShell.xaml / AppShell.xaml.cs: Defines the overall structure and navigation of your app.
- MauiProgram.cs: Configures the .NET MAUI application, including services and handlers.
- MainPage.xaml / MainPage.xaml.cs: The default page displayed when the app starts.
Next Steps
Congratulations on setting up .NET MAUI and creating your first app! From here, you can start exploring:
- UI Basics: Learn about layouts, controls, and styling.
- Data Binding: Understand how to connect your UI to your data.
- Navigation: Implement navigation between pages.
- Architecture: Dive deeper into the MAUI architecture.