Как я могу заставить Xamarin Forms закрывать открытые пролистанные представления? - PullRequest

1 Ответ

2 голосов
/ 14 июля 2020

SwipView имеет методы SwipeStarted и SwipeEnded, с их помощью можно закрыть предыдущий открытый элемент.

Например, добавление SwipeStarted и SwipeEnded в Xaml ( код на основе официального образца ) следующим образом:

...
<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>
  ...

Объявите List<SwipeView> в ContentPage. При вызове SwipeEnded добавьте элемент в 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);
}

Эффект:

введите описание изображения здесь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...