简体   繁体   中英

Onappearing not working when modal page is popped in Xamarin iOS

I have an onappearing method which doesn't fire when page is popped in Xamarin iOS but works fine in Xamarin Android.Could anyone please help me with this. Thank you.

Page 1 (Tabbed Page)

 protected override void OnAppearing()
    {
        base.OnAppearing();

            if (CVItems.SelectedItem != null)
                CVItems.SelectedItem = null;
    }

    private async void CVItems_SelectionChangedAsync(object sender, SelectionChangedEventArgs e)
    {
        var selecteditem = e.CurrentSelection.First() as Item;
        var detailPage = new AdPage(selecteditem);
        detailPage.BindingContext = selecteditem;
        await Navigation.PushModalAsync(detailPage);             
    }

Page 2 (Modal Page)

    private async void poppage_Tapped(object sender, EventArgs e)
    {
        await Navigation.PopModalAsync();
    }

When an item in collection view is tapped, modal page opens up and then when I pop back to the tabbed page, the onappearing() method is not triggered.

Please could you show an example of you code?

You could try the following try catch to view any error that is happening:

public void OnAppearing() //could add a protected override
{
            try
            {
                OnAppearing();

            }
            catch (Exception Ex)
            {
                Debug.WriteLine(Ex.Message);
            }
}

You could have a tru wtih updating the version of Xamarin Forms to the latest version to check whether it works.

I have checked in the latest version of Xamarin Forms( 5.0.0.1931 ) and iOS( 14.4 ), it works as expected no matter TabbedPage or ContentPage (Child page of TabbedPage).

Based on this offical sample to modify code to test.

  • Step one: Press OnUpcomingAppointments Button to next Page.

  • Step two: Press Back button then back to SchedulePage .

在此处输入图像描述

TabbedPage :

public partial class MainPage : TabbedPage
{
    public MainPage ()
    {
        InitializeComponent ();
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        Console.WriteLine("TabbedPage OnAppearing");
    }
}

SchedulePage :

public partial class SchedulePage : ContentPage
{
    public SchedulePage ()
    {
        InitializeComponent ();
    }

    async void OnUpcomingAppointmentsButtonClicked (object sender, EventArgs e)
    {
        await Navigation.PushModalAsync (new UpcomingAppointmentsPage ());
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        Console.WriteLine("SchedulePage OnAppearing");
    }
}

The output:

2021-02-12 11:03:03.800726+0800 TabbedPageWithNavigationPageiOS[5537:75334] TabbedPage OnAppearing
2021-02-12 11:03:03.801024+0800 TabbedPageWithNavigationPageiOS[5537:75334] SchedulePage OnAppearing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM