.net Maui, viewModel alternative to OnAppearing

Heinz Deubler 181 Reputation points
2023-03-03T19:42:54.74+00:00

I notice that in .net MAUI OnAppearing doesn;t work. I need to find a way to open up a "DisplayAlert" as soon as the app appears and it must be triggered from the ViewModel.

Thank you in advanced.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,838 questions
{count} votes

Accepted answer
  1. Alessandro Caliaro 4,181 Reputation points
    2023-03-04T07:46:59.08+00:00

    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());
                }
            }
    
    
          
    
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Max Borrmann 5 Reputation points
    2023-03-21T15:48:04.75+00:00

    Alternatively, if you are using a BaseViewModel with MVVM and a BasePage you can declare an abstract method in your BaseViewModel and define the method in your inheriting ViewModels. You then call your OnAppearing method only in the BasePage class, so its technically not 100% MVVM but relatively close.

    in BasePageViewModel.cs

    public abstract partial class BasePageViewModel : ObservableObject 
    {        
        public abstract Task OnAppearing();
    }
    
    

    in BasePage.xaml.cs

    private async void BasePage_Appearing(object sender, EventArgs e)    
    {            
        await _vm.OnAppearing();   
    }
    

    in BasePage.xaml

    <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"                    x:Class="myNameSpace.Views.BasePage"                 
    xmlns:viewmodels="clr-namespace:myNameSpace.ViewModels"             
    x:DataType="viewmodels:BasePageViewModel"             
    Appearing="BasePage_Appearing"             
    Disappearing="ContentPage_Disappearing"                 
    ControlTemplate="{DynamicResource BasePage}" />
    

    That way you dont have to use EventToCommandBehaviour and you force all your ViewModels to do the page initialisation in the same method. If not all pages have to implement a loading event you can also make the method virtual instead abstract to make it optional.