Logo

dev-resources.site

for different kinds of informations.

Adding Platform-Specific Code to a .NET MAUI Project

Published at
10/30/2024
Categories
dotnet
maui
mobile
Author
tmcd054
Categories
3 categories in total
dotnet
open
maui
open
mobile
open
Author
7 person written this
tmcd054
open
Adding Platform-Specific Code to a .NET MAUI Project

.NET Multi-platform App UI (MAUI) is a powerful framework for building cross-platform applications with a single codebase. However, there are times when you need to write platform-specific code to leverage native APIs or functionalities. In this blog post, we’ll explore how to add platform-specific code to a .NET MAUI project, with examples on how to register and use this code on both Android and iOS platforms. We’ll also discuss a caveat regarding the difficulty of testing platform-specific code compared to cross-platform code.

Why Use Platform-Specific Code?

While .NET MAUI provides a unified API for most functionalities, certain features or optimizations might only be available on specific platforms. For instance, you might want to access a native API that isn’t exposed through MAUI, or you might need to implement platform-specific optimizations for performance reasons.

Adding Platform-Specific Code

To add platform-specific code in a .NET MAUI project, you can use partial classes, conditional compilation, or dependency injection. Let’s dive into each method with examples.

1. Partial Classes

Partial classes allow you to split the implementation of a class across multiple files. This is useful for separating platform-specific code.

Example:

Create a partial class in the shared project:

// Shared project
public partial class PlatformService
{
    public void ShowPlatformMessage();
}

Enter fullscreen mode Exit fullscreen mode

Implement the platform-specific code in the platform projects:

// Android project
public partial class PlatformService
{
    public void ShowPlatformMessage()
    {
        Android.Widget.Toast.MakeText(
        Android.App.Application.Context, 
        "Hello from Android!", 
        Android.Widget.ToastLength.Short).Show();
    }
}

Enter fullscreen mode Exit fullscreen mode
// iOS project
public partial class PlatformService
{
    public void ShowPlatformMessage()
    {
        var alert = new UIKit.UIAlertView(
        "Hello from iOS!", "", null, "OK", null);
        alert.Show();
    }
}

Enter fullscreen mode Exit fullscreen mode

2. Conditional Compilation

Conditional compilation allows you to include or exclude code based on the target platform. However, in my experience, using conditional compilation can make the code harder to read, especially in larger classes.

Example:

public void ShowPlatformMessage()
{
#if ANDROID
    Android.Widget.Toast.MakeText(Android.App.Application.Context, "Hello from Android!", Android.Widget.ToastLength.Short).Show();
#elif IOS
    var alert = new UIKit.UIAlertView("Hello from iOS!", "", null, "OK", null);
    alert.Show();
#endif
}

Enter fullscreen mode Exit fullscreen mode

3. Dependency Injection

Dependency injection is a design pattern that allows you to inject dependencies into a class, making it easier to manage platform-specific implementations. I often found myself using a mixture of the separate file and dependency injection approaches.

Example:

Define an interface in the shared project:

// Shared project
public interface IPlatformService
{
    void ShowPlatformMessage();
}

Enter fullscreen mode Exit fullscreen mode

Implement the interface in the platform projects:

// Android project
public class AndroidPlatformService : IPlatformService
{
    public void ShowPlatformMessage()
    {
        Android.Widget.Toast.MakeText(Android.App.Application.Context, "Hello from Android!", Android.Widget.ToastLength.Short).Show();
    }
}

Enter fullscreen mode Exit fullscreen mode
// iOS project
public class iOSPlatformService : IPlatformService
{
    public void ShowPlatformMessage()
    {
        var alert = new UIKit.UIAlertView("Hello from iOS!", "", null, "OK", null);
        alert.Show();
    }
}

Enter fullscreen mode Exit fullscreen mode

Register the platform-specific implementations in the MauiProgram.cs file:

// MauiProgram.cs
public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .ConfigureServices(services =>
            {
#if ANDROID
                services.AddSingleton<IPlatformService, AndroidPlatformService>();
#elif IOS
                services.AddSingleton<IPlatformService, iOSPlatformService>();
#endif
            });

        return builder.Build();
    }
}

Enter fullscreen mode Exit fullscreen mode

Use the platform-specific service in your shared code:

// Shared project
public class MainPageViewModel
{
    private readonly IPlatformService _platformService;

    public MainPageViewModel(IPlatformService platformService)
    {
        _platformService = platformService;
    }

    public void ShowMessage()
    {
        _platformService.ShowPlatformMessage();
    }
}

Enter fullscreen mode Exit fullscreen mode

Caveat: Testing Platform-Specific Code

One important caveat to keep in mind is that platform-specific code can be more challenging to test compared to cross-platform code. This is because platform-specific code often relies on native APIs and behaviours that are not easily replicated in a test environment. To mitigate this, you can:

  • Use dependency injection to abstract platform-specific code, making it easier to mock and test.
  • Write unit tests for the shared logic and use integration tests for platform-specific code.
  • Utilise platform-specific testing tools and frameworks to test native functionalities.

Upcoming Changes in .NET MAUI 9

In .NET MAUI 9, Microsoft is reintroducing a separate project approach for platform-specific code, which feels more like the Xamarin way. This change will likely alter how you interact with your platform-specific code, making it more organised and potentially easier to manage. This approach can help in keeping platform-specific code isolated, improving readability and maintainability.

Conclusion

Adding platform-specific code to a .NET MAUI project allows you to leverage the full power of native APIs and functionalities. By using partial classes, conditional compilation, and dependency injection, you can effectively manage platform-specific implementations. However, be aware of the challenges in testing platform-specific code and plan your testing strategy accordingly. With the upcoming changes in .NET MAUI 9, the process of handling platform-specific code will evolve, offering a more structured approach.

Happy coding! 🚀

maui Article's
30 articles in total
Favicon
Sneak Peek at 2024 Volume 4: .NET MAUI
Favicon
Open Source Tools for .NET MAUI
Favicon
How to Build a .NET MAUI Barcode Scanner Using IronBarcode
Favicon
Adding Platform-Specific Code to a .NET MAUI Project
Favicon
How to build a MAUI app in Azure Devops
Favicon
Create a .NET MAUI Bubble Chart to Visualize Market Cap Distribution by Country – GDP and Company Analysis
Favicon
Develop .NET MAUI Apps Using VS Code Without Traditional IDEs
Favicon
Embracing the Blazor Hybrid Approach in .NET MAUI
Favicon
The Luhn's algorithm in practice
Favicon
Chart of the Week: Visualizing Top 25 Largest Countries Using .NET MAUI Column Chart
Favicon
Free online Maui discussion 09/12/24 10:00am CST
Favicon
Design a Timer App using .NET MAUI Radial Gauge and Timer Picker
Favicon
Making Payments Easy: Using .NET MAUI Radio Button for Payment Apps and Digital Wallets
Favicon
Chart of the Week: Create .NET MAUI Multi-Category Bar Charts to Compare the Use of Generative AI for Creating Media vs....
Favicon
Introducing the New .NET MAUI Digital Gauge Control
Favicon
Mastering Shopping: Enhancing Your Product Search with the .NET MAUI CheckBox
Favicon
Introducing the 12th Set of New .NET MAUI Controls and Features
Favicon
Microsoft Build 2024: The Syncfusion Experience
Favicon
What’s New in .NET MAUI Charts: 2024 Volume 2
Favicon
Enhance PDF Viewing and Editing with the New Built-in Toolbar in .NET MAUI PDF Viewer
Favicon
Building a .NET TWAIN Document Scanner Application for Windows and macOS using MAUI
Favicon
Chart of the Week: Creating the .NET MAUI Radial Bar to Visualize Apple’s Revenue Breakdown
Favicon
Building .NET MAUI Barcode Scanner with Visual Studio Code on macOS
Favicon
Easily Export .NET MAUI DataGrid to Specific PDF Page
Favicon
Chart of the Week: Creating a .NET MAUI Doughnut Chart to Visualize the World’s Biggest Oil Producers
Favicon
Microsoft Build 2024: Key Takeaways and Insights
Favicon
Chart of the Week: Creating the .NET MAUI Scatter Chart to Visualize Different Sports Ball Sizes and Weights
Favicon
Syncfusion Essential Studio 2024 Volume 2 Is Here!
Favicon
OCR in .NET MAUI
Favicon
Sneak Peek at 2024 Volume 2: .NET MAUI

Featured ones: