🔍 How to Access OnAppearing Directly in .NET MAUI ViewModels Using MVVM and EventToCommandBehavior

Ali RazaAli Raza
2 min read

If you've been working with .NET MAUI (or even Xamarin.Forms), you're probably familiar with the OnAppearing() method in your ContentPage. It’s handy — but putting logic there breaks the MVVM pattern we developers love for keeping our code clean and testable.

Wouldn’t it be great if we could handle OnAppearing directly inside the ViewModel? Good news: we can!

Let’s walk through how to do this the right way using the .NET MAUI Community Toolkit.

âś… Prerequisites

Before getting started, make sure you’ve:

  1. Add the Community Toolkit:

    CommunityToolkit.Maui

  2. Add the Community Toolkit:

    builder.UseMauiApp().UseMauiCommunityToolkit();

🎯 Goal

We want to run logic in our ViewModel when the page appears — without writing any logic in the code-behind.

đź§© XAML Setup

In your ContentPage, declare the namespace and add EventToCommandBehavior inside the <ContentPage.Behaviors> section.

xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
<ContentPage ...
             x:Class="YourApp.Views.SamplePage"
             x:Name="this">
    <ContentPage.Behaviors>
        <!-- Bind Appearing event to VM command -->
        <toolkit:EventToCommandBehavior 
            EventName="Appearing"
            Command="{Binding AppearingCommand}"
            BindingContext="{Binding Path=BindingContext, Source={x:Reference this}, x:DataType=ContentPage}" />
    </ContentPage.Behaviors>

    <!-- Your page layout here -->

</ContentPage>

Key Details:

  • EventName="Appearing": This hooks into the ContentPage.Appearing lifecycle event.

  • BindingContext: Needed to properly bind to the ViewModel since behaviors do not inherit the page’s BindingContext by default.

  • x:Reference this: Gets the current page’s BindingContext and passes it correctly.

đź§  ViewModel Logic

In your ViewModel, define the command you want to execute on OnAppearing:

using CommunityToolkit.Mvvm.ComponentModel;

public class SampleViewModel : ObservableObject
{
    public SampleViewModel()
    { }

 [RelayCommand]
 public async void Appearing()
 {
  // logic for onAppearing
 }

}

And That’s It!

Now, every time the page appears, your AppearingCommand gets called — no code-behind needed, and your ViewModel stays in charge, just like it should in a proper MVVM setup.

🚀 Bonus: Cleaner Code, Better Architecture

This approach:

  • Keeps your code-behind completely empty or minimal

  • Makes unit testing easier (you can test AppearingCommand directly)

  • Keeps your app scalable and maintainable

📌 Summary

To recap, using EventToCommandBehavior from the .NET MAUI Toolkit allows you to elegantly bridge UI events with ViewModel logic. Handling the OnAppearing event in ViewModels this way leads to better-structured and more testable applications.

Let your UI be dumb, and your ViewModels smart!

0
Subscribe to my newsletter

Read articles from Ali Raza directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ali Raza
Ali Raza

🚀 Tech Lead | .NET MAUI Expert | Mobile App Developer I'm Ali Raza, a passionate Tech Lead with over 6 years of experience in mobile app development. I specialize in .NET MAUI/Xamarin and have led multiple high-impact projects, including enterprise apps, fintech solutions, and eSIM technology. 🔹 What I Do: ✔ .NET MAUI & Xamarin – Building cross-platform apps with robust architectures ✔ eSIM & Fintech Apps – Leading innovations in digital connectivity & finance ✔ Performance Optimization – Creating high-quality, scalable mobile solutions ✔ Mentorship & Community Sharing – Helping developers master .NET MAUI 📢 Sharing Weekly Insights on .NET MAUI/Xamarin to help developers level up! Follow me for deep dives into Native Interop, API Optimization, and Advanced UI/UX techniques. Let’s connect and build something amazing! 🚀