Forum Xamarin.Forms

OnAppearing event does not fire when a ContentPage pushed via Navigation.PushAsync

VibhavRVibhavR Member ✭✭

Hi,

I have a ContentPage being rendered by calling Navigation.PushAsync. The OnAppearing event is not firing when the AddLocationPage is loaded/rendered.

Any assistance would be appreciated! I've been sitting with this for the last two days.

public partial class App : Xamarin.Forms.Application
{
    public App()
        {
            InitializeComponent();

            MainPage = new NavigationPage(new MainTabbedPage()); // MainTabbedPage : Xamarin.Forms.TabbedPage 
        }
}
-- This ContentPage sits within a Xamarin.Forms.TabbedPage as the first TabbedPage.Children <mainTabs:LocationsPage />
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LocationsPage : ContentPage
{
    public LocationsPage()
    {
        InitializeComponent();
        NavigationPage.SetHasNavigationBar(this, false);
    }

    private async void OnListItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        if (e.SelectedItem != null)
        {
            await Navigation.PushAsync(new AddLocationPage
            {
                BindingContext = e.SelectedItem as LocationDbModel
            });
        }   
    }
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class AddLocationPage : ContentPage
{
    public AddLocationPage()
    {
        InitializeComponent();
        NavigationPage.SetHasNavigationBar(this, false);
    }

    // ISSUE: OnAppearing event not being called
    // This page gets invoked/pushed using Navigation.PushAsync from LocationsPage.OnListItemSelected()
    protected override void OnAppearing()
    {
        base.OnAppearing();

        var binddingContextData= (LocationDbModel)BindingContext;

         if ((binddingContextData == null || binddingContextData.Latitude == 0) && binddingContextData.Longitude == 0)
            return;

        // removed for brevity - This code needs to be executed when the page is loaded.
    }
}

Best Answer

Answers

Sign In or Register to comment.