Skip to Content
Course content

127: An Overview of .NET MAUI for Cross-Platform Apps

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Imagine you've designed a high-resolution vector logo for a client. You don't create a separate image for their business cards, another for their website, and another for a giant billboard. Instead, you create one "master" design. When it's time to publish, the printing software handles the specifics: it scales the logo for the card, optimizes the pixels for the web, and adjusts the resolution for the vinyl billboard. The intent is the same, but the implementation changes based on the medium.

That is exactly how .NET MAUI (Multi-platform App UI) works. You aren't writing four different apps for iOS, Android, Windows, and macOS. You're creating a "master design" in C# and XAML, and MAUI handles the translation to the native controls of each operating system.

One Blueprint, Four Different Foundations

In the old days, if you wanted a button on an iPhone and a button on an Android device, you had to use two different languages and two different sets of libraries. MAUI abstracts this. When you define a Button in MAUI, you aren't telling the computer to draw a generic rectangle; you're telling MAUI, "I need a button here."

  • On iOS: MAUI tells the OS to render a UIButton.
  • On Android: It renders an android.widget.Button.
  • On Windows: It uses WinUI 3.

I've spent years jumping between frameworks, and the beauty of MAUI is that it doesn't try to "mimic" the look of these platforms with custom drawings (which often feel "uncanny" or slightly off to the user). It uses the actual native components, so your app feels like it belongs on the device.

Designing the Look with XAML

While you can build your entire UI in C#, most of us use XAML (Extensible Application Markup Language). It's essentially XML that describes your layout. It keeps your "view" separate from your "logic," which saves you a massive headache when you need to change the UI without breaking the backend code.

Take a look at this snippet for a simple Project Timer interface. Notice how we use layouts like VerticalStackLayout to organize elements without worrying about the exact pixel coordinates of a specific screen:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ProjectTimer.MainPage">
    <VerticalStackLayout Padding="30" Spacing="25">
        <Label Text="Active Project: Website Redesign" 
               FontSize="24" 
               HorizontalOptions="Center" />
        
        <Label x:Name="TimerLabel" 
               Text="00:00:00" 
               FontSize="48" 
               FontAttributes="Bold" 
               HorizontalOptions="Center" />

        <Button Text="Start Timer" 
                Clicked="OnStartClicked" 
                BackgroundColor="DarkBlue" 
                TextColor="White" />
    </VerticalStackLayout>
</ContentPage>

If you ran this on an iPad, the VerticalStackLayout would center everything across a wide screen. On a narrow Android phone, it would stack them tightly. You write the markup once; the framework handles the geometry.

The Handlers Under the Hood

You might be wondering: "If it's all abstracted, how do I do something that only exists on one platform?" Say you need to access a very specific iOS-only API for the camera or a Windows-specific registry key. This is where "Handlers" come in.

MAUI uses a handler architecture that maps the cross-platform control to the native one. I usually tell my students to think of Handlers as the "translators." If you ever find that the default translation isn't specific enough, you can write a "Platform-Specific" override. You can literally wrap code in #if ANDROID or #if IOS blocks within your C# files to execute code only on that specific OS. It gives you the speed of cross-platform development without the "glass ceiling" of limited functionality.




📋 Practical Task

Build a Platform-Aware Device Dashboard

Your goal is to create a single-page MAUI application that identifies which operating system is currently running and displays a custom message based on that platform.

Requirements:

  • Create a ContentPage with a VerticalStackLayout.
  • Add a Label that will display the platform name (e.g., "You are running this on Android").
  • Add a Button labeled "Check System".
  • In the C# code-behind (the .xaml.cs file), implement the button click event. Use the DeviceInfo.Current.Platform property from the Microsoft.Maui.Devices namespace to determine the current OS.
  • Update the label's text based on the platform detected. For an extra challenge, change the TextColor of the label to a different color depending on the OS (e.g., Green for Android, Blue for iOS, Red for Windows).
Rating
0 0

There are no comments for now.

to be the first to leave a comment.