0

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)

2 Answers2

2

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
}
Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
0

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(); 
    }
}
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • thanks for the quick reply. But I wanted to keep the navigation bar on top of each child pages. And also when I use this approach the `Title` below the child pages icon uses the title I set on my ContentPage. Also my child pages has links that open up another page using `Navigation.PushAsync` –  Jan 09 '17 at 11:49
  • 1
    @iamsophia You can keep the navigation page in that case, the `OnAppearing` event all still work. It is *not* tied to `Page` creation/destruction, it is not a **life** cycle event, it is a **view** cycle event, i.e. On iOS it is linked `ViewDidAppear` and that will fire when the "view" is displayed, no matter if it is the first time or hundred time. On Android it is linked to the FragmentManager and will fire each time that View is display. Look at the PageRenderer code for each platform and you will see how/when is fired (view dependent, not creation dependent). – SushiHangover Jan 09 '17 at 12:33