0

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

  • if the command is in the root of your VM then you will need to specify a source when binding – Jason Apr 02 '22 at 14:13

1 Answers1

0

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);

    }
Jessie Zhang -MSFT
  • 9,830
  • 1
  • 7
  • 19