Chapter 3: Understanding .NET MAUI Project Structure

In this chapter, we'll walk through the default structure of a .NET MAUI application, explore platform-specific folders, and understand the role of App.xaml and MainPage.xaml. You'll also be introduced to the MVVM (Model-View-ViewModel) pattern, which is highly recommended for building scalable and maintainable applications.

πŸ“ Exploring the Default Project Structure

The main folders generated in a .NET MAUI app include:

MyMauiApp/ β”œβ”€β”€ Platforms/ β”‚ β”œβ”€β”€ Android/ β”‚ β”œβ”€β”€ iOS/ β”‚ β”œβ”€β”€ MacCatalyst/ β”‚ └── Windows/ β”œβ”€β”€ Resources/ β”‚ β”œβ”€β”€ Fonts/ β”‚ β”œβ”€β”€ Images/ β”‚ └── Raw/ β”œβ”€β”€ App.xaml β”œβ”€β”€ App.xaml.cs β”œβ”€β”€ MainPage.xaml β”œβ”€β”€ MainPage.xaml.cs └── MauiProgram.cs

βš™οΈ Platform-Specific Configurations

Each folder under Platforms/ provides per-OS features and settings like:

  • Android: AndroidManifest.xml, splash screen config, permissions
  • iOS: Info.plist and launch storyboards
  • Windows: Package.appxmanifest, images, tile settings
  • MacCatalyst: Similar to iOS, but for macOS apps

🧩 Understanding App.xaml and MainPage.xaml

App.xaml is used to define shared resources like global styles and colors:

<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui" x:Class="MyMauiApp.App"> <Application.Resources> <Style TargetType="Label"> <Setter Property="TextColor" Value="DarkSlateBlue" /> </Style> </Application.Resources> </Application>

App.xaml.cs is where you set the startup page for your app:

public partial class App : Application { public App() { InitializeComponent(); MainPage = new MainPage(); } }

MainPage.xaml is your first UI screen. Here’s a basic example:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" x:Class="MyMauiApp.MainPage"> <VerticalStackLayout Spacing="25" Padding="30"> <Label Text="Welcome to .NET MAUI!" FontSize="32"/> <Button Text="Click Me!" Clicked="OnCounterClicked"/> </VerticalStackLayout> </ContentPage>

πŸ“ Working with MVVM Pattern

MVVM promotes separation of concerns and better maintainability. Below is a simple ViewModel:

public class MainViewModel : INotifyPropertyChanged { private string _greeting; public string Greeting { get => _greeting; set { _greeting = value; OnPropertyChanged(); } } public ICommand SayHelloCommand { get; } public MainViewModel() { Greeting = "Welcome!"; SayHelloCommand = new Command(() => Greeting = "Hello from MVVM!"); } public event PropertyChangedEventHandler PropertyChanged; void OnPropertyChanged([CallerMemberName] string name = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); }

Set this ViewModel in your code-behind like this:

public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); BindingContext = new MainViewModel(); } }

And bind it to XAML controls:

<Label Text="{Binding Greeting}" FontSize="24" /> <Button Text="Say Hello" Command="{Binding SayHelloCommand}" />

πŸ”š Summary

You now understand how .NET MAUI projects are structured, what each folder and file does, and how to implement MVVM for real-world app development. This knowledge is foundational for building scalable, clean, and cross-platform apps with ease.