Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Xamarin.Forms] Is there a way to cancel a tab switch after clicking a selectable tab? #58

Open
AdamDiament opened this issue Oct 31, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@AdamDiament
Copy link

AdamDiament commented Oct 31, 2022

Given this UI:

<tabs:ViewSwitcher x:Name="Switcher"
                              SelectedIndex="{Binding SelectedViewModelIndex, Mode=TwoWay}">

               ...some views

</tabs:ViewSwitcher>

and

   <tabs:TabHostView x:Name="TabHost"
                             TabType="Fixed"
                             SelectedIndex="{Binding Source={x:Reference Switcher}, Path=SelectedIndex, Mode=TwoWay}">
                <tabs:TabHostView.Tabs>

                 ... some tabs

                </tabs:TabHostView.Tabs>
     </tabs:TabHostView>

and this view model implementation:

    public int SelectedViewModelIndex
    {
        get => selectedViewModelIndex;
        set
        {

               if (ShouldChangePage(from: selectedViewModelIndex, to: value)){
                      SelectedViewModelIndex = value;
              }{
              // Don't want to change tab if I get here
              }

        }
    }

Even if the pseudo "ShouldChangePage" method returns false, and we never actually set the SelectedViewModelIndex property - the UI switches tabs anyway even though the selectedViewModelIndex doesn't change.

I could utilise IsEnabled and pre-disable the tabs based on my logic - but I would like to perform an action based on a tab click like show some UI with "why can't you switch" type explanation.

Is there any way to do this?

@AdamDiament AdamDiament changed the title [Xamarin.Forms] Is there a way to cancel a tab switch after clicking? [Xamarin.Forms] Is there a way to cancel a tab switch after clicking an enabled tab? Oct 31, 2022
@AdamDiament
Copy link
Author

AdamDiament commented Oct 31, 2022

Looking into the source, It appears that OnTabItemTapped is always called and the selected index always changed in TabHostView whenever an item is tapped and IsSelectable is true. And this is what drives the tab to change. The view model index is just reacting to that property, and not changing it doesn't affect the underlying view's new tab index. So I don't think there is any way to do this without using IsSelectable and pre-calculating the selectable status. Would I be correct @roubachof ?And would you consider a PR to add an event to be raised when a user clicks a "non-selectable" tab so I could perform some action without the UI changing?

@AdamDiament AdamDiament changed the title [Xamarin.Forms] Is there a way to cancel a tab switch after clicking an enabled tab? [Xamarin.Forms] Is there a way to cancel a tab switch after clicking a selectable tab? Oct 31, 2022
@EmmanVazz
Copy link

I found that clearing the Effects for the tabitem in the TabHostView.Tabs array blocks navigating to the tab and allows any GestureRecognizers on that tabitem to work. This is because when you click on a selectable tab it adds the effect

private void AddTapCommand(TabItem tabItem)
but never clears them. So if you make the tab to IsSelectable = false you will have to manually clear those effects to get it to not navigate.

Also if you want to which that tab back to IsSelectable = true you need to trigger UpdateSelectableTabs to get the proper behavior. The easiest way I got it to do that was just to change the orientation and change it back in the next line to the original value. It doesn't show any UI flickering when you do that thankfully. Hope that helps anyone in the future.

@roubachof roubachof added the bug Something isn't working label May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants