Overview
Software development is evolving in such a way that cross-platform development is becoming increasingly important. In this article, we present a comprehensive overview of the features and capabilities of .NET MAUI (Multi-platform App UI), a robust solution for developing cross-platform desktop applications with C# and .NET 8.
Introduction to .NET MAUI
The technology allows developers to write code once and deploy it across multiple operating systems with .NET MAUI, which is an evolution of Xamarin.Forms. This reduces development time and improves consistency by allowing developers to write code once and deploy it across multiple operating systems.
Setting Up Our Development Environment
Before diving into .NET MAUI, make sure you have the necessary tools installed. You can use Visual Studio 2022 or Visual Studio Code with the .NET MAUI workload installed. Follow the official documentation for more information.
Creating a .NET MAUI Desktop Project
Start by making a .NET MAUI desktop application in your preferred development environment.
dotnet new maui-desktop -n ZiggyRafiq.CrossPlatformDesktopApp
cd ZiggyRafiq.CrossPlatformDesktopApp
This creates a new .NET MAUI desktop project named " ZiggyRafiq.CrossPlatformDesktopApp."
Designing the User Interface
The .NET MAUI project structure supports both desktop and mobile platforms. The application's user interface is defined in XAML, which provides a declarative way to describe its visual structure.
In the MainPage folder, open the MainPage.xaml file and modify it as follows:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ZiggyRafiq.CrossPlatformDesktopApp.MainPage"
BackgroundColor="{DynamicResource PageBackgroundColor}">
<ScrollView>
<VerticalStackLayout
Padding="30,0"
Spacing="25">
<Image
Source="dotnet_bot.png"
HeightRequest="185"
Aspect="AspectFit"
SemanticProperties.Description="dot net bot in a race car number eight" />
<Label
Text="Welcome to .NET MANU Desktop by Ziggy Rafiq!"
Style="{StaticResource Headline}"
FontSize="50"
SemanticProperties.HeadingLevel="Level1"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"
/>
<Label
Text="Welcome to .NET Multi-platform App UI by Ziggy Rafiq"
Style="{StaticResource SubHeadline}"
SemanticProperties.HeadingLevel="Level2"
SemanticProperties.Description="Welcome to dot net Multi platform App U I Example by Ziggy Rafiq" />
<Button
x:Name="CounterBtn"
Text="Click me"
SemanticProperties.Hint="Counts the number of times you click"
Clicked="OnCounterClicked"
HorizontalOptions="Fill" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>
Handling Platform-Specific Code
Despite its general focus on code sharing, .NET MAUI provides platform-specific projects to cater to scenarios requiring platform-specific code.
Navigate to the MauiProgram.cs file and change the title as follows:
using Microsoft.Extensions.Logging;
namespace ZiggyRafiq.CrossPlatformDesktopApp
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
#if DEBUG
builder.Logging.AddDebug();
#endif
return builder.Build();
}
}
}
In this example, the ConfigureMauiWinUI method configures a specific Windows platform setting.
Running Your .NET MAUI Desktop App
The next step is to run your .NET MAUI desktop application after you've designed your user interface and handled platform-specific code. The following command will do it:
dotnet maui check
dotnet maui build
dotnet maui run
MAUI desktop apps can be built and run on the platform of your choice using this command.
Summary
.NET MAUI provides a unified and efficient development environment for building cross-platform desktop applications using C# and .NET 8. .NET MAUI offers shared codebases, consistent UIs, and the ability to handle platform-specific requirements for developers of desktop applications today. Using .NET MAUI, you can create visually appealing, powerful cross-platform desktop applications.
Please do not forget to like this article if you have found it useful and follow me on my LinkedIn https://www.linkedin.com/in/ziggyrafiq/ also, I have uploaded the source code for this article on my GitHub Repo: https://github.com/ziggyrafiq/CrossPlatform-CSharp-NET8-MAUI-DesktopApps and recently I have published a book on Amazon “Understanding C#12 Coding Standards, Best Practices, and Standards in the Industry: Developing Robust and Maintainable Code in Today's Development Environment” recommend you reading it as it provides lots of code examples and industry standards and best practices using C# 12.