5

I have a Tabbed Page Xamarin Form application that I am trying to customize. I am migrating from an IOs only app to all platforms. In iOS there are a couple function that I used ViewDidDisappear(), ViewDidAppear() and ViewDidLoad();

The application today loads the tabbed page as follows:

{
    Children.Add(new MyPage1());
    Children.Add(new MyPage2());
    Children.Add(new MyPage3());
    Children.Add(new MyPage4());
}

Each of the child pages are declared as follows all inheriting from ContentPage.

class MyPage1: ContentPage
{
        …
        protected override void OnAppearing()
        {
            base.OnAppearing();
            Content = SomeContentPage;
        }
}

The problem that I am running into is that the OnAppearing() is called for all pages when only MyPage1 is currently displayed. I need to know when each child page is loaded.

I have read about message center but I am not sure how to implement it in this particular case. Would the message center be the best solution for this?

How do I implement a solution that will allow me to know which of the four pages is displayed?

Andrew K
  • 121
  • 1
  • 4

2 Answers2

3

Possible workaround would be to override OnCurrentPageChanged in your TabbedPage.

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

    if ( CurrentPage is YourContentPage page)
    {
        page.YourCustomCode();
    }
}
mklieber
  • 192
  • 11
-1

On the main TabbedPage, after you've added child pages, setting the CurrentPage property to null should fix the problem.

chaosifier
  • 2,666
  • 25
  • 39
  • @Shockwaver, Actually it does fix the problem. Don't know why it didn't work in your case. https://forums.xamarin.com/discussion/comment/318378#Comment_318378 – chaosifier May 15 '18 at 14:20
  • This addresses the previous bug of the first page's `OnAppearing` firing twice. Does not fix all pages' `OnAppearing`s firing when the app starts. – Bondolin Apr 14 '21 at 13:54