Home > Software engineering >  How to call the previous page function in MAUI?
How to call the previous page function in MAUI?

Time:09-27

My problem: My parent page (AdminPanel.xaml) contains a function: updateAllMaterialList()

It's the page previous one on the navigation stack.

Now on the AddUser.xaml page, I try to navigate back by calling updateAllMaterialList() like this:

    private async void GoToBack(object sender, EventArgs e)
    {
    await Navigation.PopAsync();

    NavigationPage navPage = (NavigationPage)App.Current.MainPage;
    Page page = navPage.Navigation.NavigationStack[navPage.Navigation.NavigationStack.Count - 1];
    ((AdminPanel)page).updateAllMaterialList();

    }

But it doesn't work

CodePudding user response:

As Jason said, you can Generate OnAppearing method override in AdminPanel.xaml.cs, and add updateAllMaterialList() function to OnAppearing. So, whenever the AddUser.xaml page navigates to the AdminPanel.xaml page, it will call updateAllMaterialList() automatically. OnAppearing means that when the specified page appears, it will be called.

CodePudding user response:

First I want to point out, that there is good reason not keep your business logic in your pages.

I recommend that you check CommunityToolkit.MVVM/MAUI. And then add some service, to do this work for you. Or use EvenToCommandBehavior to bind your page events.(Appearing, Loading, etc..) Or use Messenger to send/receive data between decoupled segments.

The way you are attempting to handle your logic and navigation will bring a lot of problems. One page navigation should have nothing to do with another page business logic.

  • Related