Inspiration Behind the Article

This article builds upon my previous guide on developing cross-platform apps with .NET MAUI Blazor Hybrid. If you’re new to .NET MAUI or Blazor Hybrid, I highly recommend reading that guide first to grasp the foundational concepts and set up your development environment.

In this article, we’ll focus on creating a real-time currency converter app, an excellent practical use case for cross-platform applications. You’ll learn how to design and develop an app that not only fetches live exchange rates but also delivers a consistent user experience across mobile, web, and desktop platforms, all while leveraging a single UI framework: Blazor.

Here’s a preview of the app running on all three platforms web, mobile, and desktop.

Desktop

Image 1. Currency Converter on Desktop, Web, and Mobile

SRS for Currency Converter UI

Before we start building the currency converter app, it’s essential to understand how to create a Blazor Hybrid app. I covered this in my previous article, [Building Cross-Platform Apps with .NET MAUI Blazor Hybrid], so I recommend reviewing it to get started.

For this project, I have named the app ForExFlow. After creating the solution, you will have the following three projects.

Solution explorer

Image 2: Solution Explorer - Project Name: ForExFlow

The Service

Create a new folder named "Services" in the "ForExFlow.Shared" project and add the following interface, IForexRateService, and the class, ForexRateService.

IForexRateService: Service interface

Let's start with the service interface. It's quite simple; all we need is a single method that takes a currency as input and returns a dictionary containing country names and their respective exchange rates.

public interface IForexRateService
{
    Task<Dictionary<string, decimal>> GetExchangeRatesAsync(string inputCurrency);
}

Code Snippet 1. Service Interface - IForexRateService.

ForexRateService Class: Fetching Exchange Rates via API

The ForexRateService class is an implementation of the IForexRateService interface, we need this class to fetch the live exchange rates from an API (https://api.exchangerate-api.com/v4/latest).

1. Private Field

2. Constructor

3. GetExchangeRatesAsync

4. ExchangeRateResponse: Mapping API Response.

The UI: ForexComponent.razor

Create a new component "ForexComponent.razor" under the pages folder in the "ForExFlow.Shared" project.

1. Currency Selection Dropdown

2. User Input for Units

3. Exchange Rates Table

Code-Behind Logic: ForexComponent.razor.cs

1. Dependency Injection: Injecting the Forex Service

2. Variables

3. Methods

Mobile and Desktop app

This is the beauty of this architecture; we are done with the core code. Now, we just need to reference the code.

This will ensure that the ForexRateService is available to be injected into components throughout the mobile and desktop app. Once the service is registered, the app is ready to fetch and display exchange rates across both platforms.

 Mobile app

Image 3. Mobile app: Currency Converter

Desktop app

Image 4. Desktop app: Currency Converter

Web app

To achieve the same functionality in the web app, expand the ForExFlow.Web project and open the Program.cs file. Add the same line of code to register the Forex service as we did for the mobile and desktop apps. This ensures that the web application also has access to the exchange rate service.

Webapp

Image 5. Webapp: Currency Converter

Conclusion

In this article, we built a cross-platform currency converter using the MAUI Blazor Hybrid Web App, which allows it to run on the web, desktop, and mobile. We implemented a service to fetch real-time exchange rates.

By leveraging Blazor’s component-based architecture and .NET MAUI’s cross-platform capabilities, we could develop a unified codebase for multiple platforms.