The OnAppearing is an event you can use in the "codebehind" file.
So, if you have a MainPage.xaml, you can override the OnAppearing in MainPage.xaml.cs file.
If you want to use OnAppearing inside the ViewModel file, you can use the Maui Community Toolkit
<ContentPage.Behaviors>
<mct:EventToCommandBehavior Command="{Binding AppearingCommand}" EventName="Appearing" />
<mct:EventToCommandBehavior Command="{Binding DisappearingCommand}" EventName="Disappearing" />
</ContentPage.Behaviors>
and in your ViewModel
[RelayCommand]
void Appearing()
{
try
{
// DoSomething
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
[RelayCommand]
void Disappearing()
{
try
{
// DoSomething
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}