Featured image of post Should You Use MVVM with MAUI?

Should You Use MVVM with MAUI?

Alternatives Explored and Compared with Code Examples

Introduction

So, you’re building a .NET MAUI app, and you’re wondering: Should I use MVVM?

Short answer: Maybe.

Long answer: It depends on your project. While MVVM (Model-View-ViewModel) is the go-to architecture for XAML-based applications, alternatives like MVC, MVP, and even Blazor Hybrid offer different benefits.

Let’s dive deep into MVVM, explore its history, pros and cons, and compare it with alternatives like MVC, MVP, and Blazor Hybrid, complete with code examples.


A Brief History of MVVM

What is MVVM?

MVVM (Model-View-ViewModel) was first introduced by Microsoft for WPF (Windows Presentation Foundation) and later became the de facto standard for XAML-based UI frameworks, including Xamarin and MAUI.

Why was MVVM created?

Before MVVM, developers used code-behind files in XAML applications, leading to tight coupling between UI and business logic. MVVM solves this by:

  • Keeping UI (View) separate from logic (ViewModel)
  • Supporting data binding to reduce UI updates manually
  • Making unit testing easier by separating UI logic

What Are the Alternatives to MVVM?

ArchitectureDescription
MVVMData-binding friendly, widely used in XAML apps
MVC (Model-View-Controller)Common in web apps, but possible in MAUI
MVP (Model-View-Presenter)Similar to MVVM but more explicit
Blazor HybridUses Razor components instead of XAML

Comparing MVVM, MVC, MVP, and Blazor Hybrid

FeatureMVVMMVCMVPBlazor Hybrid
Separation of ConcernsStrongModerateStrongStrong
Data BindingExcellentLimitedLimitedExcellent
Ease of TestingHighModerateHighModerate
ComplexityModerateHighHighLow
Best ForXAML AppsWeb & HybridXAML AppsHybrid & Web Apps

Code Examples

MVVM Example in MAUI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// ViewModel (Bindable to UI)
public class MainViewModel : INotifyPropertyChanged
{
    private string _message;
    public string Message
    {
        get => _message;
        set { _message = value; OnPropertyChanged(); }
    }

    public Command UpdateMessageCommand { get; }

    public MainViewModel()
    {
        UpdateMessageCommand = new Command(() => Message = "Hello from MVVM!");
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

MVC Example in MAUI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// Controller-like logic inside the View's code-behind
public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    private void OnButtonClick(object sender, EventArgs e)
    {
        messageLabel.Text = "Hello from MVC!";
    }
}

MVP Example in MAUI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// Presenter
public class MainPresenter
{
    private readonly IMainView _view;

    public MainPresenter(IMainView view)
    {
        _view = view;
        _view.ButtonClicked += () => _view.ShowMessage("Hello from MVP!");
    }
}

// View Interface
public interface IMainView
{
    event Action ButtonClicked;
    void ShowMessage(string message);
}

Blazor Hybrid Example in MAUI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
@page "/"
@inject MyService myService

<h1>@message</h1>
<button @onclick="UpdateMessage">Click me</button>

@code {
    private string message = "Hello, Blazor!";
    
    private void UpdateMessage() {
        message = "Hello from Blazor Hybrid!";
    }
}

Pros and Cons of Each Approach

ArchitectureProsCons
MVVMGreat for XAML, easy data bindingRequires more boilerplate
MVCGood separation of concernsCan be complex
MVPMore explicit than MVVMExtra interfaces needed
Blazor HybridLeverages web techNot as mature as MVVM

When Should You Use MVVM?

Use MVVM if:

  • You’re building a MAUI or WPF app using XAML.
  • You want clean separation of UI and logic.
  • You need two-way data binding.

Consider alternatives if:

  • You’re comfortable with MVC from web development.
  • You prefer Blazor Hybrid for a web-like experience.
  • You don’t need data binding.

Key Ideas

  • MVVM is the standard for XAML-based apps like MAUI and WPF.
  • MVC is possible but feels more natural for web apps.
  • MVP offers more explicit separation but adds complexity.
  • Blazor Hybrid is great for developers who love Razor components.
  • Choose the right architecture based on your project needs!

References

  1. MVVM in .NET MAUI
  2. MVC vs MVVM vs MVP
  3. Blazor Hybrid Apps
  4. Microsoft Docs - MAUI
  5. XAML Data Binding