Xamarin Forms:如何从 SwipeStarted 事件中获取 SwipeDirection 值?

Xamarin Forms: How to get the SwipeDirection value from SwipeStarted event?

我在我的项目中使用 swipeview 功能来滑动 flowlistviews。我的中心流程列表视图需要左右滑动。我怎样才能在 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。

你最好在DataTemplate上添加SwipeGestureRecognizer而不是整个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 的情况下处理代码隐藏中的逻辑。您应该使用滑动事件而不是命令。

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

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