如何使 Xamarin Forms 关闭打开的滑动视图?

Ala*_*an2 1 xamarin xamarin.forms

我在 CollectionView 中使用 SwipeView:

在此输入图像描述

           <CollectionView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" ItemsSource="{Binding ListOfDecks}" 
                                SelectionMode="None">
                <CollectionView.ItemsLayout>
                    <LinearItemsLayout ItemSpacing="0" Orientation="Vertical"/>
                </CollectionView.ItemsLayout>
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <SwipeView>
                            <SwipeView.RightItems>
                                <SwipeItems Mode="Reveal" SwipeBehaviorOnInvoked="Auto">
                                    <SwipeItem Command="{Binding BindingContext.DeleteDeckCmd, Source={x:Reference ThisPage}}" CommandParameter="{Binding .}" Text="aa" BackgroundColor="Red" />
                                    <SwipeItem Command="{Binding BindingContext.RenameDeckCmd, Source={x:Reference ThisPage}}" CommandParameter="{Binding .}" Text="xx" BackgroundColor="LightGray"/>
                                </SwipeItems>
                            </SwipeView.RightItems>
                            <t:DeckGridTemplate />

                        </SwipeView>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
Run Code Online (Sandbox Code Playgroud)

当我向左滑动时效果很好。但是当我滑动另一行时,第一行不会关闭。

所以最后,如果我不进去并手动关闭所有滑动的行,我的用户体验看起来会非常糟糕。有没有一种方法可以在运行另一个滑动时自动关闭一个滑动,并且在进入下一个屏幕并返回到此屏幕时也可以关闭滑动?

Jun*_*ang 6

SwipViewSwipeStartedSwipeEnded方法,使用它们可以关闭以前打开的项目。

例如,在XamlSwipeStarted中添加和(代码基于官方示例)如下:SwipeEnded

...
<SwipeView SwipeStarted="SwipeView_SwipeStarted" SwipeEnded="SwipeView_SwipeEnded">
            <SwipeView.LeftItems>
                <SwipeItems SwipeBehaviorOnInvoked="Close"
                            Mode="Reveal">
                    <SwipeItem Text="Favorite"
                                IconImageSource="favorite.png"
                                BackgroundColor="LightGreen"
                                Command="{Binding Source={x:Reference collectionView}, Path=BindingContext.FavoriteCommand}"
                                CommandParameter="{Binding}" />
                    <SwipeItem Text="Delete"
                                IconImageSource="delete.png"
                                BackgroundColor="LightPink"
                                Command="{Binding Source={x:Reference collectionView}, Path=BindingContext.DeleteCommand}"
                                CommandParameter="{Binding}" />
                </SwipeItems>
            </SwipeView.LeftItems>
  ...
Run Code Online (Sandbox Code Playgroud)

List<SwipeView>在 ContentPage 中声明 a 。调用时SwipeEnded,将 item 添加到List<SwipeView>. 稍后调用时SwipeStarted,关闭并删除前一项。

List<SwipeView> swipeViews { set; get; }
public VerticalListSwipeContextItemsPage()
{
    InitializeComponent();
    BindingContext = new MonkeysViewModel();

    swipeViews = new List<SwipeView>();
}

private void SwipeView_SwipeStarted(object sender, SwipeStartedEventArgs e)
{
    Console.WriteLine("SwipeView_SwipeStarted");

    if(swipeViews.Count == 1)
    {
        swipeViews[0].Close();
        swipeViews.Remove(swipeViews[0]);
    }
}

private void SwipeView_SwipeEnded(object sender, SwipeEndedEventArgs e)
{
    Console.WriteLine("SwipeView_SwipeEnded");
    swipeViews.Add(swipView);
}
Run Code Online (Sandbox Code Playgroud)

效果:

在此输入图像描述