4

I am working on xamarin.forms. I need to detect the event of tab being changed either by swapping left or right or by clicking without using custom renderer

I tried below event but it is firing in both cases when child page being pushed or tab being changed. how can i get isolated event of tab being changed

public class MyTabbedPage : TabbedPage
{
   public MyTabbedPage()
   {
      this.CurrentPageChanged += CurrentPageHasChanged;
   }
   protected void CurrentPageHasChanged(object sender,EventArgs e)
   {
       var pages= Navigation.NavigationStack;
       if (pages.Count > 0)
       {
           this.Title = pages[pages.Count - 1].Title;
       }
       else
       this.Title = this.CurrentPage.Title;
   }
}

This issue I am facing is: In below screenshot part1 is Homepage(title="Diary") & part2 is Childpage(title="Homework") when I change tab & again come to first tab than navigationbar title getting changed "Homework" to "Diary"(Screeshot2)

enter image description here

enter image description here

R15
  • 13,982
  • 14
  • 97
  • 173
  • 2
    I don't remember how exactly it works for Xamarin but try checking the `sender` and `e` in debug mode and see if there's any difference for two scenarios you've described... – Fabjan Jun 12 '18 at 10:59
  • `sender` having `CurrentPage` sender-->base-->base-->CurrentPage. But I am not able get that page name. This can help me. – R15 Jun 12 '18 at 11:42

2 Answers2

11

As you are on your tabbed page already you can literally just do the following

public partial class MyTabbedPage : TabbedPage
{
    public MyTabbedPage()
    {
        InitializeComponent();          
        CurrentPageChanged += CurrentPageHasChanged;
    }

    private void CurrentPageHasChanged(object sender, EventArgs e) => Title = CurrentPage.Title;
}

if you want to use the sender you can do the following

public partial class MyTabbedPage : TabbedPage
{
    public MyTabbedPage()
    {
        InitializeComponent();          
        CurrentPageChanged += CurrentPageHasChanged;
    }

    private void CurrentPageHasChanged(object sender, EventArgs e) 
    { 
        var tabbedPage = (TabbedPage) sender;
        Title = tabbedPage.CurrentPage.Title;
    }
}

Or if you can elaborate on what you are trying to do exactly I can give a better answer as I do not know what it is that you are trying to do exactly

Carodice
  • 256
  • 4
  • 6
1

I don't think you can achieve what you want to do, at least not like this. The event behaves the way it does and as described: it is fired whenever the current page changes, also for children.

That being said, I think you should focus on implementing the functionality you want with the tools we have. I can't really deduce from your code what you are trying to do, but it looks like you want to change the title? When a tab is changed? Why not just make some kind of condition to only do it for certain pages? For example when the page is contained in the Children collection?

Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100
  • The issue i am facing, I tried to explain in updated question. I can't make condition for certain pages I can have multiple pages in app. As you said I am not able to make condition like collection containing _Child_, This is the basic thing why not available in xamarin.forms? – R15 Jun 12 '18 at 11:30