user6742877
user6742877

Reputation:

OnAppearing in children of TabbedPage is triggered only once

I have a TabbedPage in Xamarin.Forms:

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

       var playPage = new NavigationPage(new PlayPage())
       {
          Title = "Play",
          Icon = "play1.png"
       };
       var settingsPage = new NavigationPage(new SettingsPage())
       { 
          Title = "Settings",
          Icon = "settings.png"
        };
        var aboutPage = new NavigationPage(new AboutPage())
        {
           Title = "About",
           Icon = "about.png"
        };
        Children.Add(playPage);
        Children.Add(settingsPage);
        Children.Add(aboutPage);
    }

Each of the child pages are a ContentPage that overrides the OnAppearing method. The content of my child pages are not updating properly when I navigate between the tabs and further debugging tells me that the OnAppearing method for child pages are only called once (when the MainPage is first loaded).

Anyone have any idea why the OnAppearing method is only called once? And how can I solve this issue?

More Info

My child pages such SettingsPage contains events that when fired open another ContentPage using Navigation.PushAsync method. I expected the OnAppearing method to also get called when switching from tab pages and the navigation pages within those tab pages. (Hope this makes sense)

Upvotes: 0

Views: 2664

Answers (2)

SushiHangover
SushiHangover

Reputation: 74144

Do not embedded your ContentPages within a "single" level NavigationPage.

The following works fine in terms of the OnAppearing event firing when switching between tabs:

public class PlayPage : ContentPage
{
    protected override void OnAppearing()
    {
        System.Diagnostics.Debug.WriteLine("PlayPage");
        base.OnAppearing();
    }
}
public class AboutPage : ContentPage
{
    protected override void OnAppearing()
    {
        System.Diagnostics.Debug.WriteLine("AboutPage");
        base.OnAppearing();
    }
}
public class SettingsPage : ContentPage
{
    protected override void OnAppearing()
    {
        System.Diagnostics.Debug.WriteLine("SettingPage");
        base.OnAppearing();
    }
}

public partial class MainPage : TabbedPage
{
    public MainPage()
    {
        var playPage = new PlayPage() { Title = "Play" };
        var settingsPage = new SettingsPage() { Title = "Settings" };
        var aboutPage = new AboutPage() { Title = "About" };
        Children.Add(playPage);
        Children.Add(settingsPage);
        Children.Add(aboutPage);
    }
}

public class App : Application
{
    public App()
    {
        this.MainPage = new MainPage(); 
    }
}

Upvotes: 0

Cheesebaron
Cheesebaron

Reputation: 24460

It only triggers once since the View is not destroyed when you navigate between tabs.

You can use the CurrentPageChanged event to figure our that a page has changed and signal the view it changes to and do whatever update of the view you need.

this.CurrentPageChanged += PageChanged;

void PageChanged(object sender, EventArgs args)
{
    var currentPage = CurrentPage as MyTabPage;
    currentPage?.UpdateView();
}

Then in your page:

public void UpdateView()
{
    // do whatever you need here to update the page
}

Upvotes: 2

Related Questions