In .Net Maui what is the equivalent of the Xamarin.Forms ViewIsAppearing Function

Matthew Robb 21 Reputation points
2022-12-05T12:18:11.613+00:00

Hi

I have just switched from Xamarin.Forms to .Net Maui. and I am trying to reconfigure a working Xamarin.Forms App.

It uses MVVM.

Previously I used protected override void ViewIsAppearing to make adjustments to the page before showing it. This does not seem to exist in .net Maui, is there a way to detect the Page is appearing event?

Thanks for any help

Matt

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Matthew Robb 21 Reputation points
    2022-12-05T17:09:25.91+00:00

    For Info I have resolved this. It was a case of insufficient knowledge!

    More detail. Previously I had used FreshMVVM for Xamarin, but I switched to the Community.Toolkit.MVVM for the conversion. I needed to update the ViewModel's List selection mode before displaying the main page.

    Using XF I used protected override void ViewIsAppearing in the ViewModel to detect the ViewIsAppearing Event. So I tried using the .Net Maui OnAppearing in the MainViewModel. It wasn't recognised! But I noted that I could use OnAppearing in the MainPage XAML code behind page.

    However (see image attached) I wasn't picking up the ViewModel reference in the OnAppearing Function. So I had to reference the ViewModel in that function. This is quite an ugly solution, as code I would have liked in the ViewModel is now in the code behind page. With FreshMVVM I never put any extra code in the XAML code behind which was neater. I appreciate FreshMVVM is available for MAUI but I will press on with Community.Toolkit.MVVM.

    After all this, the upload docs wouldnt work. So I have included a snippet below. Hope this helps some other MAUI newbie!

    public partial class MainPage : ContentPage
    {

    public MainPage(MainViewModel vm)
    {
    // This approach is in keeping with the CommunityToolkit.ComponentModel
    InitializeComponent();
    BindingContext = vm
    }

    //My mental block was that this was previously in the ViewModel for Xamarin
    override protected void OnAppearing()
    {
    Base.OnAppearing();
    var vm = BindingContext as MainViewModel; //Note the binding context here was essential.
    vm.DummyData = “Yeh! OnAppearing Worked Correctly!”;
    }

    }


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.