Search code examples
xamarinxamarin.formsswipecardview

How to get value of swiped card view xamarin


I have a SwipeCardView and I want to get the id of the user that I've just swiped.

<swipeCardView:SwipeCardView
        x:Name="SwipeView1" ItemsSource="{Binding Profiles}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
        Padding="10" >
        <swipeCardView:SwipeCardView.ItemTemplate>
            <DataTemplate>
                <StackLayout HorizontalOptions="FillAndExpand"
                                 VerticalOptions="FillAndExpand">
                    <Frame CornerRadius="10"
                                   Padding="8"
                                   HorizontalOptions="FillAndExpand"
                                   VerticalOptions="FillAndExpand">
                        <AbsoluteLayout>
                            <AbsoluteLayout.GestureRecognizers>
                                <SwipeGestureRecognizer Direction="Left" Command="{Binding LeftCommand}"
                                                                         CommandParameter="{Binding nome}" />
                            </AbsoluteLayout.GestureRecognizers>
                            <Image Source="{Binding foto_perfil}"
                                           Aspect="AspectFill"
                                           AbsoluteLayout.LayoutBounds=".5,0.5,1,1"
                                           AbsoluteLayout.LayoutFlags="All" />
                            <Label FontSize="Large"
                                       WidthRequest="30"
                                           FontAttributes="Bold"
                                           TextColor="White"
                                           BackgroundColor="Black"
                                           AbsoluteLayout.LayoutBounds="0.1,0.95,250,30"
                                           AbsoluteLayout.LayoutFlags="PositionProportional">
                                <Label.FormattedText>
                                    <FormattedString>
                                        <Span Text="{Binding nome}" />
                                        <Span Text=", " />
                                        <Span Text="{Binding data_nasc}" />
                                    </FormattedString>
                                </Label.FormattedText>
                            </Label>
                        </AbsoluteLayout>
                    </Frame>

                </StackLayout>
            </DataTemplate>
        </swipeCardView:SwipeCardView.ItemTemplate>
    </swipeCardView:SwipeCardView>

In the C# I created a command but it never enter i the void:

public Command LeftCommand => new Command<string>(LeftSwipe);
        void LeftSwipe(string parameter)
        {
            var variable = parameter; //= breed_Name
            
            DisplayAlert("", variable.ToString(), "ok");
        }

I don´t know what I've done wrong because it still swipes


Solution

  • If your LeftCommand is in the ViewModel of your page, you can specify a source just as Jason said.

    You can refer to the folloing code:

            <swipeCardView:SwipeCardView x:Name="mCardView"
                ItemsSource="{Binding CardItems}"
                VerticalOptions="FillAndExpand">
                <swipeCardView:SwipeCardView.ItemTemplate>
                    <DataTemplate>
    
                        <StackLayout >
                            <Label Text="{Binding Name}" FontSize="Large" HorizontalTextAlignment="Center"     VerticalTextAlignment="Center" BackgroundColor="Beige" />
                            <StackLayout.GestureRecognizers>
                                <SwipeGestureRecognizer Direction="Left"
                                Command="{Binding Path=BindingContext.LeftCommand, Source={x:Reference mCardView}}"  
                                CommandParameter="{Binding .}" >
                                </SwipeGestureRecognizer>
                            </StackLayout.GestureRecognizers>
                        </StackLayout>
                        
                    </DataTemplate>
                </swipeCardView:SwipeCardView.ItemTemplate>
            </swipeCardView:SwipeCardView>
    

    Note:

    1.mCardView is the x:Name="mCardView" of your SwipeCardView;

    2.In your viewModel, you can get the Binded Item as follows:

        public Command LeftCommand => new Command(LeftSwipe);
        void LeftSwipe(Object parameter)
        {
            //You can change `Profile ` to your Item Model
            Profile variable = parameter as Profile; 
    
            System.Diagnostics.Debug.WriteLine("-----------> " + variable.Name +"<---> Id = "+ variable.ProfileId);
    
        }