Loading...

Modern Cross-Platform App Development with .NET MAUI


Discover how .NET MAUI is transforming modern cross-platform app development.

Introduction to .NET MAUI: A Guide to Cross-Platform Application Development

In today's world of mobile and desktop application development, creating separate codebases for different platforms is often a time-consuming and costly process. This is where Microsoft offers a powerful solution that allows you to develop iOS, Android, Windows, and macOS applications from a single codebase: .NET MAUI (Multi-platform App UI).

In this blog post, we'll comprehensively cover what .NET MAUI is, its core features, how to start your project, and what to consider when developing a simple application.

What is .NET MAUI?

.NET MAUI is an open-source UI framework for building cross-platform applications with XAML and C#. Developed as the evolution of Xamarin.Forms, MAUI comes with .NET 6 and later versions, empowering you to create modern, native-looking and feeling applications. Its main goal is to increase developer productivity by enabling them to target all major platforms from a single project.

Key Features of .NET MAUI

Here are some key features that make .NET MAUI stand out:

  • Single Project Experience: Instead of dealing with separate projects for each platform, you can now manage all platform targets within a single .NET MAUI project. This greatly simplifies development, build, and deployment processes.

  • Native Interfaces: .NET MAUI uses the platform's own native controls. This ensures your application delivers a natural look and feel on each platform, improving user experience without compromising performance.

  • C# and XAML: You can write powerful backend logic with C# code and define declarative UI with XAML. This flexibility allows developers to work with their preferred approach.

  • Hot Reload: Instantly see changes made to your XAML and C# code without having to stop and rebuild your application. This dramatically speeds up the UI development process.

  • .NET Ecosystem Integration: .NET MAUI works seamlessly with existing .NET libraries, NuGet packages, and tools. This allows you to leverage the advantages of the extensive .NET community and resources.

Getting Started with .NET MAUI

To start developing .NET MAUI applications, you need the following prerequisites:

  • Visual Studio 2022 (Version 17.3 or later): With the “.NET Multi-platform App UI development” workload installed.

  • .NET SDK: The required .NET SDK version usually comes bundled with Visual Studio.

Creating a new .NET MAUI project is quite straightforward:

  1. Open Visual Studio 2022.

  2. Select “Create a new project”.

  3. Search for and select the “.NET MAUI App” template from the list.

  4. Give your project a name and specify its location.

  5. Click “Create”.

A Simple .NET MAUI Application: Structure and Usage

When you create a new .NET MAUI project, you'll find a basic folder structure and some predefined files:

  • MauiProgram.cs: This is the entry point of your application. You configure fonts, dependency injection, and other global settings here.

  • App.xaml and App.xaml.cs: Defines your application's lifecycle and global resources (styles, templates).

  • MainPage.xaml and MainPage.xaml.cs: Contains the UI (XAML) and code-behind logic (C#) for your application's main page.

  • Platforms folder: Houses platform-specific code or resources. You typically won't need to edit this directly.

Here's the XAML and C# code for a simple “Hello World!” application:

MainPage.xaml:

<!-- MainPage.xaml -->
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyMauiApp.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Padding="30,0"
            Spacing="25">
            <Image
                Source="dotnet_bot.png"
                HeightRequest="185"
                Aspect="AspectFit"
                SemanticProperties.Description="dot net bot in a hovercraft number nine" />

            <Label
                Text="Hello World!"
                FontSize="32"
                HorizontalOptions="Center" />

            <Label
                Text="Welcome to Your MAUI App!"
                FontSize="18"
                HorizontalOptions="Center" />

            <Button
                x:Name="CounterBtn"
                Text="Click Me"
                SemanticProperties.Hint="Increments the click count on the button"
                Clicked="OnCounterClicked"
                HorizontalOptions="Fill" />
        </VerticalStackLayout>
    </ScrollView>
</ContentPage>

MainPage.xaml.cs:

namespace MyMauiApp;

public partial class MainPage : ContentPage
{
    int count = 0;

    public MainPage()
    {
        InitializeComponent(); // Initializes XAML components
    }

    private void OnCounterClicked(object sender, EventArgs e)
    {
        count++;

        if (count == 1)
            CounterBtn.Text = $"Clicked {count} time";
        else
            CounterBtn.Text = $"Clicked {count} times";

        SemanticScreenReader.Announce(CounterBtn.Text);
    }
}

In this example, a VerticalStackLayout is used to create a vertical arrangement, containing an Image, two Labels, and a Button. The Clicked event of the Button is handled by the OnCounterClicked method in C#, which increments a counter and updates the Button's text.

Why Use .NET MAUI?

  • Single Codebase, Multiple Platforms: Significantly reduces development time and cost.

  • Native Performance and Experience: Ensures your applications feel fast and natural on every platform.

  • Extensive .NET Support: Allows you to reuse your existing .NET skills and libraries.

  • Rapid Development Cycle: Features like Hot Reload enable instant visualization of UI changes.

  • Microsoft Support and Community: Strong Microsoft backing and a growing developer community make it easier to find solutions to issues.

Conclusion

.NET MAUI is an ambitious and powerful solution in the realm of cross-platform application development. Its ability to create modern, performant, and native-looking applications from a single codebase makes it an attractive option for developers today. Whether you're building mobile or desktop applications, .NET MAUI provides the tools you need to accelerate your development process and expand your audience.

Start a .NET MAUI project today and discover the power of cross-platform development!