首页 > 解决方案 > 如何在滑动视图中获取属性的值

问题描述

所以我有一个Swipeview,如果我向左滑动,我想得到狗的名字

<swipeCardView:SwipeCardView.ItemTemplate>
                <DataTemplate>
                    <StackLayout HorizontalOptions="FillAndExpand"
                                     VerticalOptions="FillAndExpand">
                        <Frame CornerRadius="10"
                                       Padding="8"
                                       HorizontalOptions="FillAndExpand"
                                       VerticalOptions="FillAndExpand">
                                <AbsoluteLayout>
                                <AbsoluteLayout.GestureRecognizers>
                                    <SwipeGestureRecognizer Direction="Left" Swiped="SwipeGestureRecognizer_Swiped" />
                                    <SwipeGestureRecognizer Direction="Right" Swiped="SwipeGestureRecognizer_Swiped_1" />
                                    <SwipeGestureRecognizer Direction="Up" Swiped="SwipeGestureRecognizer_Swiped_2" />
                                </AbsoluteLayout.GestureRecognizers>
                                <Image Source="{Binding dogImage}"
                                               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 dogGender}" />
                                                <Span Text=", " />
                                                <Span Text="{Binding breed_Name}" />
                                            </FormattedString>
                                        </Label.FormattedText>
                                    </Label>
                                </AbsoluteLayout>
                            </Frame>

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

我想获取Text="{Binding breed_Name}在滑动视图中向左滑动并将其保存在变量中的值。

我怎么做?

标签: c#xamlxamarinxamarin.forms

解决方案


使用Command而不是Swiped事件并将其发送到CommandParameter

<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 breed_Name}"/>
                          ...
                                        <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 dogGender}" />
                                                    <Span Text=", " />
                                                    <Span Text="{Binding breed_Name}" />
                                                </FormattedString>
                                            </Label.FormattedText>
                                     </Label>
                        </AbsoluteLayout>
...
public Command LeftCommand => new Command<string>(LeftSwipe);

void LeftSwipe(string parameter) 
{
    var variable = parameter; //= breed_Name
}

推荐阅读