Posted in:

Android and iOS App Development with C# and .NET MAUI

Mobile apps have become ubiquitous, with Android and iOS dominating the market. Building native mobile apps requires learning platform-specific languages like 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 MAUI lets you reuse 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 a unified way for .NET web development teams 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 for entering mobile app development without learning new languages and platforms. Hire ASP.NET developers with a background in ASP.NET web development allows leveraging existing talent to deliver mobile apps with .NET MAUI and web applications.

With a single C# codebase, .NET MAUI can render performant native UIs on each platform while sharing most of the business logic. This maximizes code reuse between .NET web development and mobile app projects, making app development tremendously efficient. .NET MAUI integrates deeply with Visual Studio on Windows and Mac, providing a great IDE experience for .NET developers. For .NET web development teams looking to expand to 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 across platforms -Native UIs on each platform for a natural look and feel
  • Access native device capabilities like camera, GPS, etc.
  • Single 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 library for app logic
  • Platform-specific projects for UI and OS integration
  • XAML for declaring UI with data bindings to code
  • Native rendering of UI controls on each platform

This architecture allows maximizing code reuse while still providing native performance and UX.

Setting 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 layout. Views contain pages with controls like buttons, lists, etc.
  • View code-behind contains UI logic handling events like clicks.
  • Services abstract platform-specific logic like navigation, messaging, etc.

That’s the basic structure. The same C# code powers UIs on Android and iOS thanks to .NET MAUI’s cross-platform rendering.

Building the User Interface

The biggest benefit of .NET MAUI is being able to define UI declaratively using XAML and reuse it across platforms. For example:

<ContentPage xmlns=”http://schemas.microsoft.com/dotnet/2021/maui”

             xmlns:x=”http://schemas.microsoft.com/winfx/2009/xaml”

             x:Class=”App.MainPage”>

    <ScrollView>

        <VerticalStackLayout 

            Spacing=”25″ Padding=”30,0″ VerticalOptions=”Center”>

            <Image Source=”dotnet_bot.png” SemanticProperties.Description=”Cute dot net bot waving hi to you!”/>

            <Label Text=”Hello .NET MAUI!” 

                   SemanticProperties.HeadingLevel=”Level1″

                   FontSize=”32″ HorizontalOptions=”Center” />

            <Label Text=”Welcome to cross-platform apps” 

                   SemanticProperties.HeadingLevel=”Level2″

                   FontSize=”18″ HorizontalOptions=”Center” />

            <Button Text=”Click me”

                    SemanticProperties.Hint=”Tells the system to perform the button’s click action”

                    Clicked=”OnButtonClicked” 

                    HorizontalOptions=”Center” />

        

        </VerticalStackLayout>

    

    </ScrollView>

</ContentPage>

This declarative XAML UI can be rendered natively on Android and iOS with full access to platform features.

Responding to User Interaction

To handle user events like button clicks, we can write C# code in the code-behind:

namespace App;

public partial class MainPage : ContentPage

{

  public MainPage()

  {

    InitializeComponent();

  }

  private void OnButtonClicked(object sender, EventArgs e)

  {

    DisplayAlert(“Button Clicked”, “You clicked the button!”, “OK”);

  }

}

All the application logic can be written in standard C# without platform-specific code.

Accessing Native Features

While the UI and logic code is fully portable, we can access native features using the .NET MAUI APIs:

// Vibrate

Vibration.Vibrate(); 

// Get location

var location = await Geolocation.GetLocationAsync();

// Take and save photo

var photo = await MediaPicker.CapturePhotoAsync();

await 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 produces native binary executables for optimum performance:

  • Android: .apk package ready for publishing to the Play Store
  • iOS: XCode project to publish to the App Store

It ensures your app is indistinguishable from native apps built using Kotlin or Swift.

Conclusion

In conclusion, .NET MAUI provides ASP.NET developers with 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 those skills to deliver full-featured apps on Android and iOS. For companies already leveraging ASP.NET for their web applications, having developers use .NET MAUI for mobile development enables tremendous code and talent reuse. The flexibility to target the web via ASP.NET and mobile platforms with .NET MAUI is advantageous for enterprise development teams looking to expand their offering. With a unified strategy around the .NET ecosystem spanning 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 reusing their web development skills.