0

I have a .Net Maui 7 app. When a certain modal page is open, I need to open a new modal page and close the previous one. I have tried the following code but it's not working :

    Navigation.RemovePage(Navigation.ModalStack.Last());    // Remove the current page from the stack   
    await Navigation.PushModalAsync(TheNewPage, false);     // Open the new modal page  

What's happening is that the line **Navigation.RemovePage** doesn't remove the modal page from the stack. For some reasons out of this topic, I can not use Shell navigation. Does anyone know how to solve that please ?

2
  • You have to pop the existing modal and then push a new one Commented May 31, 2024 at 16:56
  • thank you @Jason , but doing that results in closing both pages Commented Jun 3, 2024 at 8:36

1 Answer 1

0

but doing that results in closing both pages

Yes, this is just the case as you said. But you can try the following workaround.

The idea is that we can first pop up the existing modal page(Let's say it's SecondPage), and once coming back to the previous page(Let's say it's MainPage), then we can push a new one modal page(Let's say it's ThirdPage) on MainPage. And we use EventHandler to handle the event coming back to MainPage from SecondPage.

Please refer to the following code:

MainPage.xaml.cs

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        SecondPage.mEventHander += delegate (object s, int value) {
            BackCall(s, value);
        };
    }

    private async void BackCall(object s, int a)
    {  
        //push a new modal page once coming back to current page
        await Navigation.PushModalAsync(new ThirdPage());

    }

    private void Button_Clicked(object sender, EventArgs e)
    {   // navigate to SecondPage
        Navigation.PushModalAsync(new SecondPage());
    }
}

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:c="clr-namespace:MauiApp0604"
             BackgroundColor="Yellow"
              Padding="20"
             x:Class="MauiApp0604.MainPage">
        <VerticalStackLayout
            Padding="30,0"
            Spacing="25">
            <Button  Text="To second" Clicked="Button_Clicked"/>
        </VerticalStackLayout>
</ContentPage>

SecondPage.xaml.cs

public partial class SecondPage : ContentPage
{  //define mEventHander here
    public static EventHandler<int> mEventHander;

      public SecondPage()
      {
            InitializeComponent();
      }

    private  async void Button_Clicked(object sender, EventArgs e)
    {
        await Navigation.PopModalAsync();
        
        //use hander here
        EventHandler<int> handler = mEventHander;
        if (handler != null)
        {
            mEventHander(this,1);
        }
    }
}

SecondPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp0604.SecondPage"
             Title="SecondPage">
    <VerticalStackLayout>
        <Label
            Text="Welcome to second page"
            VerticalOptions="Center"
            HorizontalOptions="Center" />

        <Button Text="To third" Clicked="Button_Clicked"></Button>
    </VerticalStackLayout>
</ContentPage>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.