Featured image of post Blazor Hybrid with MAUI- Share code?

Blazor Hybrid with MAUI- Share code?

Can You Share Code with Your Website? Should You?

Introduction

So, you’ve heard about Blazor Hybrid and MAUI, and now you’re wondering:

  • Can I share code between my MAUI app and my Blazor website?
  • Should I even try?

Short answer: Yes, but…

Long answer: It depends on your project structure, goals, and how much frustration you can handle.


A Brief History of Blazor Hybrid and MAUI

Blazor: From WebAssembly to Hybrid Apps

Blazor started as a WebAssembly-based framework for running .NET in the browser. Over time, it evolved into Blazor Server, Blazor WebAssembly, and Blazor Hybrid.

MAUI: Xamarin’s Successor

.NET MAUI (Multi-platform App UI) is the evolution of Xamarin, allowing developers to build cross-platform native apps with a single codebase.

Maui covers iPhone, iPad, Android, Windows, and Mac OS.

Blazor Hybrid + MAUI: The Best of Both Worlds?

Blazor Hybrid embeds a Blazor WebView inside a MAUI app, allowing you to reuse Blazor components in desktop and mobile applications.


Pros and Cons of Blazor Hybrid with MAUI

FeatureProsCons
Code SharingReuse Blazor components across web & MAUIMay require refactoring
PerformanceRuns .NET natively, faster than WebAssemblySome overhead with WebView
UI ConsistencyBlazor UI looks the same on all platformsNot truly native UI
Ease of LearningDevelopers familiar with Blazor can reuse skillsXAML developers may struggle
FlexibilityWorks on Windows, macOS, iOS, and AndroidLimited native API access

Can You Share Code Between MAUI and Blazor?

Yes! The key is structuring your project correctly. Here’s how:

Best Way to Share Code: Use a Shared .NET Standard Library

  1. Create a .NET Standard Class Library for shared business logic.
  2. Add the shared library to both your Blazor website and MAUI app.
  3. Reference this library instead of duplicating logic.

Structuring Your MAUI + Blazor Project for Code Sharing

Project Structure Example

1
2
3
4
/MySolution
  ├── SharedLibrary (Contains shared logic)
  ├── BlazorWebsite (ASP.NET Blazor WebAssembly)
  ├── MauiApp (MAUI Blazor Hybrid App)

Shared Code Example (Inside SharedLibrary)

1
2
3
4
5
6
7
public class WeatherService
{
    public string GetWeather()
    {
        return "Sunny with a chance of Blazor!";
    }
}

Using the Shared Code in Blazor

1
2
3
@inject WeatherService WeatherService

<h1>Weather: @WeatherService.GetWeather()</h1>

Using the Shared Code in MAUI

1
2
3
var weatherService = new WeatherService();
var weather = weatherService.GetWeather();
Console.WriteLine(weather);

Should You Share Code Between MAUI and Blazor?

It depends on your project’s needs. If you’re building:

  1. A web + mobile app that shares logic → YES, share code via a shared library.
  2. A web app that looks truly native on mobile → MAYBE, but Blazor Hybrid has limitations.
  3. A performance-critical mobile app → NO, use MAUI’s native controls instead.

Alternative Approaches and Their Pros & Cons

ApproachProsCons
Blazor HybridEasy code reuseNot truly native UI
Blazor WebAssembly + APIFully web-basedRequires API for mobile
MAUI with Razor ComponentsNative UI + BlazorMore complexity
Separate MAUI & Blazor AppsBest performanceMore maintenance

Key Ideas

  • Blazor Hybrid allows Blazor components to run in MAUI apps, but it’s not truly native.
  • A shared .NET Standard library is the best way to share code.
  • If performance is a priority, MAUI’s native approach is better.
  • Blazor WebAssembly + API is a great alternative for cross-platform apps.

References

  1. Blazor Hybrid Documentation
  2. MAUI Documentation
  3. Blazor WebAssembly vs Blazor Server
  4. How to Structure a MAUI & Blazor Project
  5. Blazor Hybrid vs Blazor WebAssembly