locked
Can we override the clickevent for shell? RRS feed

  • Question

  • User368437 posted

    I'm using the shell in xamarin forms, I want to get the event of the click because I want to return to the rootpage when clicking one of the tab.

    Tuesday, June 25, 2019 5:44 PM

Answers

  • User369978 posted
    1. Create a class inherit from TabBar and override the method OnPropertyChanged (i was intend to override OnTabIndexPropertyChanged , but it was never triggered) .

       public class MyTab : TabBar
          {        
              protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
              {
                  if (propertyName == "CurrentItem")
                  {
                      int index = this.Items.IndexOf(this.CurrentItem);
                      if(index == 1)
                      {
                          //handle the stuff
                      }
      
                  }
              }
          }
      
    2. Use it in Xaml

       <local:MyTab>
          <Tab Title="Browse" Icon="tab_feed.png" x:Name="tab1">
              <ShellContent ContentTemplate="{DataTemplate local:ItemsPage}" />
          </Tab>
          <Tab Title="About" Icon="tab_about.png">
              <ShellContent ContentTemplate="{DataTemplate local:AboutPage}" />
          </Tab>
      </local:MyTab>
      
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, June 26, 2019 3:36 AM

All replies

  • User369978 posted
    1. Create a class inherit from TabBar and override the method OnPropertyChanged (i was intend to override OnTabIndexPropertyChanged , but it was never triggered) .

       public class MyTab : TabBar
          {        
              protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
              {
                  if (propertyName == "CurrentItem")
                  {
                      int index = this.Items.IndexOf(this.CurrentItem);
                      if(index == 1)
                      {
                          //handle the stuff
                      }
      
                  }
              }
          }
      
    2. Use it in Xaml

       <local:MyTab>
          <Tab Title="Browse" Icon="tab_feed.png" x:Name="tab1">
              <ShellContent ContentTemplate="{DataTemplate local:ItemsPage}" />
          </Tab>
          <Tab Title="About" Icon="tab_about.png">
              <ShellContent ContentTemplate="{DataTemplate local:AboutPage}" />
          </Tab>
      </local:MyTab>
      
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, June 26, 2019 3:36 AM
  • User368437 posted

    thank you @ColeX even thought I didn't work with your method but it gave me an idea for my case, I just added in the OnDisappearing() method the code below and it works

    protected override void OnDisappearing() { base.OnDisappearing(); try { var ShellSectionList = Shell.Current.CurrentItem.Items; if (!ShellSectionList[2].IsChecked) App.NavigateToRootAsync();

            }
            catch (Exception)
            {
            }
        }
    
    Wednesday, June 26, 2019 12:32 PM