Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to use Switch with TapGestureRecognizer in listview?

BhautikBhautik Member ✭✭✭
<ListView ItemsSource="{Binding UserList}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout>
                    <Switch OnColor="#D27474" ThumbColor="#D27474" IsToggled="{Binding IsActive}">
                        <Switch.Behaviors>
                            <behaviors:EventToCommandBehavior EventName="Toggled" Command="{Binding TestCommand}"/>
                        </Switch.Behaviors>
                    </Switch>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Whenever my Listview is initialize and my IsActive property value is initialize Toggle event is automatically called and it will goes to circular.Is there any way to prevent this?

Tagged:

Best Answers

  • BhautikBhautik Member ✭✭✭
    Accepted Answer
    > @ColeX said:
    > * We could get model inside event .
    >
    >
    > * Refer to the solution i provided above.
    >
    > static bool isFirst;private void Switch_Toggled(object sender, ToggledEventArgs e){ if (Page1.isFirst) { Page1.isFirst = false; return; } else { var s = sender as Switch; var model = s.BindingContext as Model; //here you can get parameter from model . }}

    Thanks

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    Behaviors is no need , you could place logic into setter method of IsActive property .

        public bool IsActive
        {
            set
            {
                if(value)
                {
                    Text= "On";
                }
                else
                {
                    Text = "OFF";
                }
    
                isActive = value;
                NotifyPropertyChanged();
            }
    
            get
            {
    
                return isActive;
            }
        }
    

  • BhautikBhautik Member ✭✭✭
    > @ColeX said:
    > Behaviors is no need , you could place logic into setter method of IsActive property .
    >
    > public bool IsActive { set { if(value) { Text= "On"; } else { Text = "OFF"; } isActive = value; NotifyPropertyChanged(); } get { return isActive; } }
    >
    > (Image)

    Yes I can but whenever my UserList(As mention in example) is initialize with list of data IsActivity property value will also set so at the initialisation time it will call automatically.I want to call some api on toggle event.
  • ColeXColeX Member, Xamarin Team Xamurai

    What do you want ?prevent the initial call on toggle event ?

  • BhautikBhautik Member ✭✭✭
    edited August 19
    > @ColeX said:
    > What do you want ?prevent the initial call on toggle event ?

    Yes, Is there any way to prevent this?
  • ColeXColeX Member, Xamarin Team Xamurai

    @Bhautik said:

    @ColeX said:
    What do you want ?prevent the initial call on toggle event ?

    Yes, Is there any way to prevent this?

    You could declare a static bool variable inside mode class , though this is a ugly way ..

    static bool isFirst = true;
    
    
    if(Model.isFirst){
        Model.isFirst = false;
        return;
    }
    else{
        //call api
    }
    
  • BhautikBhautik Member ✭✭✭
    > @ColeX said:
    > (Quote)
    > You could declare a static bool variable inside mode class , though this is a ugly way ..
    >
    > static bool isFirst = true;if(Model.isFirst){ Model.isFirst = false; return;}else{ //call api}

    Yes..but why TapgesturRecognizer not working for switch in listview?
  • BhautikBhautik Member ✭✭✭
    > @ColeX said:
    > (Quote)
    > You could declare a static bool variable inside mode class , though this is a ugly way ..
    >
    > static bool isFirst = true;if(Model.isFirst){ Model.isFirst = false; return;}else{ //call api}

    <ListView ItemsSource="{Binding UserList}">
    <ListView.ItemTemplate>
    <DataTemplate>
    <ViewCell>
    <StackLayout>
    <Switch OnColor="#D27474" ThumbColor="#D27474">
    <Switch.GestureRecognizers>
    <TapGestureRecognizer Command="{Binding TestCommand}"></TapGestureRecognizer>
    </Switch.GestureRecognizers>
    </Switch>
    </StackLayout>
    </ViewCell>
    </DataTemplate>
    </ListView.ItemTemplate>
    </ListView>

    Can't we use like this?
  • ColeXColeX Member, Xamarin Team Xamurai

    Switch can be toggled , why you want to add TapGestureRecognizer on it ?

  • BhautikBhautik Member ✭✭✭
    > @ColeX said:
    > Switch can be toggled , why you want to add TapGestureRecognizer on it ?

    On TapGesture I want to pass parameter UserId and call api based in that
  • ColeXColeX Member, Xamarin Team Xamurai

    Why don't you do this in Toggled event ?

  • BhautikBhautik Member ✭✭✭
    > @ColeX said:
    > Why don't you do this in Toggled event ?
    Facing two problem
    1 how can I pass parameter in toggle event?
    2 toggle event fire on first time initialization or on itemsource refresh
  • BhautikBhautik Member ✭✭✭
    Accepted Answer
    > @ColeX said:
    > * We could get model inside event .
    >
    >
    > * Refer to the solution i provided above.
    >
    > static bool isFirst;private void Switch_Toggled(object sender, ToggledEventArgs e){ if (Page1.isFirst) { Page1.isFirst = false; return; } else { var s = sender as Switch; var model = s.BindingContext as Model; //here you can get parameter from model . }}

    Thanks
  • ColeXColeX Member, Xamarin Team Xamurai

    If it helps could you accept my answer ?

  • BhautikBhautik Member ✭✭✭
    > @ColeX said:
    > If it helps could you accept my answer ?

    Done
Sign In or Register to comment.