Search code examples
c#xamarin.formsswipecardview

Set SwipeCardView SelectedItem to a variable in xamarin form


I'm trying to find a solution to my app more than 3 days, but without success.

I need to read the current user e-mail from a SwipeCardView when the user swipe or lick in a button and store it in a variable to be used later. Is there a simple solution to reach this goal?

See my xaml:

<swipeCardView:SwipeCardView 
            x:Name="SwipeCardView"
            ItemsSource="{Binding Candidates}" SwipedCommand="OnSwiped" SwipedCommandParameter="{Binding email}"
            HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
            Padding="10"
            SupportedSwipeDirections="Left,Right,Up">
            <swipeCardView:SwipeCardView.ItemTemplate>
                <DataTemplate>
                    <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                        <Frame CornerRadius="10" Padding="8" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" >
                            <AbsoluteLayout>
                                <Image Source="{Binding profilePicture}" Aspect="AspectFill" AbsoluteLayout.LayoutBounds=".5, 0.5, 1, 1" AbsoluteLayout.LayoutFlags="All"></Image>
                                <Frame x:Name="fCandiate"  CornerRadius="10" Padding="8" HeightRequest="50" AbsoluteLayout.LayoutBounds=".10, .98, 0.80, 0.20" AbsoluteLayout.LayoutFlags="All">
                                    <StackLayout>
                                        <Label Text="{Binding fullName}" TextColor="#4f4f4f" FontSize="Large" FontAttributes="Bold" />
                                        <BoxView Color="#4f4f4f" HeightRequest="2" HorizontalOptions="Fill" />
                                        <Label x:Name="candEmail" Text="{Binding email}" TextColor="#6f6f6f" FontSize="Medium" />
                                    </StackLayout>

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

And my xaml.cs

private void OnLikeClicked(object sender, EventArgs e)
        {
            string email =  "???? (read from current item on SwipeCard)";
            SwipeCardView.InvokeSwipe((MLToolkit.Forms.SwipeCardView.Core.SwipeCardDirection)MLToolkit.Forms.SwipeCardView.Core.SwipeCardDirection.Right);
            DisplayAlert("Success", "You accepted the candidate. Page will be developed later" + email, "OK");
        }

private void OnSwiped(object sender, SwipedCardEventArgs e)
        {
            switch (e.Direction)
            {
                case SwipeCardDirection.None:
                    break;
                case SwipeCardDirection.Right:
                    DisplayAlert("Success", "You accepted the candidate. Page will be developed later" + e.Parameter.ToString(), "OK");
                    break;
                case SwipeCardDirection.Left:
                    break;
                case SwipeCardDirection.Up:
                    break;
                case SwipeCardDirection.Down:
                    break;
            }
        }

Any help will be apreciated.


Solution

  • in OnLikeClicked

    var item = (MyClassName)SwipeCardView.TopItem;
    var email = item.email;
    

    or in OnSwiped

    var item = (MyClassName)e.Item;
    var email = item.email;