首页 > 解决方案 > Xamarin Forms:如何从 SwipeStarted 事件中获取 SwipeDirection 值?

问题描述

swipeview在我的项目中使用功能来刷flowlistviews. 我的中心 flowlistview 需要左右滑动。我怎样才能做到这一点swipeview

我的代码:

<SwipeView
    x:Name="SwipeView2"
    SwipeStarted="CenterSwipeView"
    IsVisible="False">
    <SwipeView.RightItems>
        <SwipeItems>
            <SwipeItem/>
        </SwipeItems>
    </SwipeView.RightItems>
    <flv:FlowListView>
        //listview items
    </flv:FlowListView>
</SwipeView>

public void CenterSwipeView(object sender, SwipeStartedEventArgs args)
{
  //how can I get the SwipeDirection value here
}

我需要根据SwipeDirection值(左或右)调用不同的函数。

另外,我试过了,SwipeGestureRecognizer但它不适用于 flowlistview。

标签: xamarin.formsswipeview

解决方案


您最好添加SwipeGestureRecognizerDataTemplate不是整个 FlowListView(否则它将与 listview 的默认滚动事件冲突)。

<DataTemplate>
    <StackLayout BackgroundColor="LightBlue" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">

        <StackLayout.GestureRecognizers>
            <SwipeGestureRecognizer Direction="Left" Command="{Binding xxx}"/>
            <SwipeGestureRecognizer Direction="Right" Command="{Binding xxx}"/>
        </StackLayout.GestureRecognizers>

        // the elements


    </StackLayout>  
</DataTemplate>

更新

由于您在不使用 MVVM 的情况下处理 Code Behind 中的逻辑。您应该使用 swipe 事件而不是 command 。

<StackLayout.GestureRecognizers>
    <SwipeGestureRecognizer Direction="Left" Swiped="SwipeGestureRecognizer_Swiped"/>
</StackLayout.GestureRecognizers>
private void SwipeGestureRecognizer_Swiped(object sender, SwipedEventArgs e)
{
   // set List2 ...
}

此外,在您的情况下,最好的解决方案是使用 Tabbed Page 。默认情况下会支持这样的功能。


推荐阅读