【问题标题】:Xamarin Forms: How to get the SwipeDirection value from SwipeStarted event?Xamarin Forms:如何从 SwipeStarted 事件中获取 SwipeDirection 值?
【发布时间】:2020-09-06 09:54:44
【问题描述】:

我在我的项目中使用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.forms swipeview


    【解决方案1】:

    最好在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 的情况下处理 Code Behind 中的逻辑。您应该使用滑动事件而不是命令。

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多