Mobile apps have become ubiquitous, with Android and iOS dominating the market. Building native mobile apps requires learning platform-specific languages, such as Kotlin for Android and Swift for iOS.
.NET Multi-platform App UI (.NET MAUI) is a new cross-platform framework from Microsoft that allows building native mobile apps for Android and iOS using C# and .NET. In this post, we’ll see how .NET lets you reuse MAUI code and skills to build production-quality apps for both platforms.
Overview of .NET MAUI
.NET MAUI represents an evolution in Microsoft’s cross-platform mobile development framework, building on Xamarin.Forms. It provides .NET web development teams with a unified way to build mobile apps for Android and iOS using C# and .NET skills. For organizations already using ASP.NET for .NET web development, .NET MAUI is the natural choice to get into mobile app development without learning new languages and platforms. Hire ASP.NET developers with a background in ASP.NET web development enables leveraging existing talent to deliver mobile apps using .NET MAUI and web applications.
With a single C# codebase, .NET MAUI can render high-performance native UIs on any platform while sharing most of the business logic. This maximizes code reuse between .NET web development and mobile app projects, making app development immensely efficient. .NET MAUI deeply integrates with Visual Studio on Windows and Mac, providing a great IDE experience for .NET developers. For .NET web development teams looking to expand into mobile, .NET MAUI is the optimal cross-platform framework to build Android and iOS apps.
Key Features and Benefits of .NET MAUI:
- Write app logic in C# and share it across platforms – Native UIs on any platform for a natural look
- Access native device capabilities such as camera, GPS, etc.
- One project and codebase for Android and iOS
- Native performance with compiled code
- Live preview and hot reload during development
- Visual Studio integration on Windows, MacOS
.NET MAUI Architecture:
- Shared .NET Standard app logic library
- Platform specific projects for UI and OS integration
- XAML for declaring the user interface with data bindings to code
- Native rendering of UI controls on any platform
This architecture allows maximizing code reuse while providing native performance and UX.
Set up a .NET MAUI app
You can use Visual Studio on Windows or MacOS to create and build .NET MAUI apps. Let’s see how to set up a simple project:
- Create a new .NET MAUI app in Visual Studio. This provides a solution template with shared and platform-specific projects.
- The main entry point is the App class in the shared project. It loads the app and defines routes.
- XAML files define the UI format. Views contain pages with controls such as buttons, lists, etc.
- View the code behind contains UI logic that handles events such as clicks.
- Services abstract platform-specific logic such as navigation, messaging, etc.
That’s the basic structure. The same C# code powers the user interfaces on Android and iOS thanks to the cross-platform rendering of .NET MAUI.
Building the user interface
The biggest advantage of .NET MAUI is that you can declaratively define the user interface using XAML and reuse it across platforms. For example:
xmlns:x=”http://schemas.microsoft.com/winfx/2009/xaml”
x:Class=”App.MainPage”>
Spacing=”25″ Padding=”30.0″ VerticalOptions=”Center”>
SemanticProperties.HeadingLevel=”Level1″
FontSize=”32″ HorizontalOptions=”Center” />
SemanticProperties.HeadingLevel=”Level2″
FontSize=”18″ HorizontalOptions=”Center” />
SemanticProperties.Hint=”Tells the system to perform the button click action”
Clicked=”ButtonClicked”
HorizontalOptions=”Middle” />
This declarative XAML user interface can be rendered natively on Android and iOS with full access to platform features.
Respond to user interaction
To handle user events like button clicks, we can write C# code in the code behind it:
namespace app;
public partial class MainPage: ContentPage
{
public home()
{
InitializeComponent();
}
private void OnButtonClicked(object sender, EventArgs e)
{
DisplayAlert(“Button clicked”, “You clicked the button!”, “OK”);
}
}
All application logic can be written in standard C# without platform specific code.
Access native features
While the UI and logic code are fully portable, we can access native functions using the .NET MAUI APIs:
// Vibrate
Vibration.Vibrate();
// Get location
var location = wait for Geolocation.GetLocationAsync();
// Take photo and save
var photo = wait for MediaPicker.CapturePhotoAsync();
wait for photo.SaveToAlbumAsync();
This provides the benefits of native apps while using a single C# codebase.
Platform specific code
For platform-specific logic, .NET MAUI provides abstraction APIs:
public interface IBrowser
{
Task OpenAsync(string URL);
}
[Android]
public class AndroidBrowser: IBrowser
{
public task OpenAsync(string URL)
{
// Android specific implementation
}
}
[iOS]
public class IosBrowser: IBrowser
{
public task OpenAsync(string URL)
{
// iOS specific implementation
}
}
The correct platform-specific implementation is injected at runtime.
Release builds
For production release builds, .NET MAUI natively produces binary executables for optimal performance:
- Android: .apk package ready for release on Play Store
- iOS: XCode project to publish on the App Store
It makes your app indistinguishable from native apps built with Kotlin or Swift.
Conclusion
In conclusion, .NET MAUI offers ASP.NET developers an incredible opportunity to expand their skills to build native mobile apps. With expertise in C#, .NET, and ASP.NET web development, developers can now use these skills to deliver full-featured apps on Android and iOS. For companies that already use ASP.NET for their web applications, using .NET MAUI for mobile development enables massive code and talent reuse. The flexibility to target the web through ASP.NET and mobile platforms with .NET MAUI is beneficial for business development teams looking to expand their offerings. With a unified strategy around the .NET ecosystem NET App development company and .NET MAUI cross-platform mobile development, organizations can deliver great experiences to users on any device. ASP.NET developers can now engage customers by building production-ready native mobile apps using C# and .NET MAUI while leveraging their web development skills.