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
Feature | Pros | Cons |
---|---|---|
Code Sharing | Reuse Blazor components across web & MAUI | May require refactoring |
Performance | Runs .NET natively, faster than WebAssembly | Some overhead with WebView |
UI Consistency | Blazor UI looks the same on all platforms | Not truly native UI |
Ease of Learning | Developers familiar with Blazor can reuse skills | XAML developers may struggle |
Flexibility | Works on Windows, macOS, iOS, and Android | Limited 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
- Create a .NET Standard Class Library for shared business logic.
- Add the shared library to both your Blazor website and MAUI app.
- Reference this library instead of duplicating logic.
Structuring Your MAUI + Blazor Project for Code Sharing
Project Structure Example
|
|
Shared Code Example (Inside SharedLibrary
)
|
|
Using the Shared Code in Blazor
|
|
Using the Shared Code in MAUI
|
|
Should You Share Code Between MAUI and Blazor?
It depends on your project’s needs. If you’re building:
- A web + mobile app that shares logic → YES, share code via a shared library.
- A web app that looks truly native on mobile → MAYBE, but Blazor Hybrid has limitations.
- A performance-critical mobile app → NO, use MAUI’s native controls instead.
Alternative Approaches and Their Pros & Cons
Approach | Pros | Cons |
---|---|---|
Blazor Hybrid | Easy code reuse | Not truly native UI |
Blazor WebAssembly + API | Fully web-based | Requires API for mobile |
MAUI with Razor Components | Native UI + Blazor | More complexity |
Separate MAUI & Blazor Apps | Best performance | More 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.