如何获取刷卡视图xamarin的价值

How to get value of swiped card view xamarin

我有一个 SwipeCardView,我想获取我刚刚刷过的用户的 id

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

在 C# 中,我创建了一个命令,但它从未输入 void:

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

我不知道我做错了什么,因为它仍然在滑动

如果您的 LeftCommand 在页面的 ViewModel 中,您可以像 Jason 所说的那样指定来源。

您可以参考以下代码:

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

注:

1.mCardView 是你的 SwipeCardView;

x:Name="mCardView"

2.In你的viewModel,你可以获取Binded Item如下:

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

    }