roubachof/Sharpnado.Presentation.Forms

SelectedTabIndexChanged

Closed this issue · 6 comments

I have noticed that the command is called several times ... how is the correct way to do it, to know which tab I have pressed?

<tabs:TabHostView.Behaviors>
<b:EventToCommandBehavior EventName="SelectedTabIndexChanged" Command="{Binding ExampleCommand}" />
</tabs:TabHostView.Behaviors>

You can bind on the IsSelected property of the TabItem.

Thanks!

@roubachof
I have a page that shares the same view and data model, I need to pass it a different parameter to retrieve different data, this is the approach I am using:

        public int SelectedViewModelIndex
        {
            get => _selectedViewModelIndex;
            set
            {
                SetProperty(ref _selectedViewModelIndex, value);
                ChatOptionsLoader.Load(LoadOptionsAsync);
            }
        }

        private async Task LoadOptionsAsync()
        {
            if (_selectedViewModelIndex is 0) 
            {
              await Service.GetInfo(User.Rooms);
            }
            else
            {
               await Service.GetInfo(User.ExitRoom);
            }
        }

use it like this:

    public int SelectedViewModelIndex
    {
        get => _selectedViewModelIndex;
        set
        {
            if (SetProperty(ref _selectedViewModelIndex, value))
            {
                ChatOptionsLoader.Load(LoadOptionsAsync);
            }
        }
    }

    private async Task LoadOptionsAsync()
    {
        if (_selectedViewModelIndex is 0) 
        {
          await Service.GetInfo(User.Rooms);
        }
        else
        {
           await Service.GetInfo(User.ExitRoom);
        }
    }

It will be called just once

thanks!

I was also surprised to discover n events triggered if n tabs are displayed.

Here is a workaround I use in my app, it's about adding a TapGestureRecognizer for each tab :

<tabs:UnderlinedTabItem.GestureRecognizers>
    <TapGestureRecognizer Command="{Binding SelectSecondTabCommand}" NumberOfTapsRequired="1" /> 
</tabs:UnderlinedTabItem.GestureRecognizers>

So that it triggers only once the targeted command with the parameters you want.

UPDATE : Sadly, this only works on iOS. Indeed, Tapped event is not raised when I run the Android version.