I'm performing swipe to delete items from a list named mylist. After an item is deleted I want to show an "UNDO" button for 10 seconds.
Here are my codes.
xaml code:
<CollectionView x:Name="list" VerticalOptions="Center">
<CollectionView.ItemTemplate>
<DataTemplate>
<SwipeView>
<SwipeView.LeftItems>
<SwipeItems Mode="Execute" SwipeBehaviorOnInvoked="Close">
<SwipeItem Text="Delete" BackgroundColor="Red" Invoked="Delete_Invoked"/>
</SwipeItems>
</SwipeView.LeftItems>
<Label Text="{Binding .}" TextColor="Black" FontSize="10" VerticalTextAlignment="Center" HorizontalTextAlignment="Start"/>
</SwipeView>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<AbsoluteLayout x:Name="undo_msg" IsVisible="false">
<ContentView x:Name="undo_item" AbsoluteLayout.LayoutBounds="0,0,1,1" HeightRequest="50" AbsoluteLayout.LayoutFlags="All" BackgroundColor="#f1f1f1" Padding="0,5,5,0">
<Grid VerticalOptions="Center" HorizontalOptions="Center" Padding="5,0,5,0">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="80"/>
</Grid.ColumnDefinitions>
<Label x:Name="undo_label" Grid.Row="0" Grid.Column="0" TextColor="Black" FontSize="16" LineBreakMode="TailTruncation" VerticalTextAlignment="Center" HorizontalTextAlignment="Start"/>
<Button Grid.Row="0" Grid.Column="1" Text="UNDO" TextColor="Blue" FontAttributes="Bold" FontSize="15"VerticalOptions="Center" HorizontalOptions="End" BorderWidth="0" BackgroundColor="Transparent"/>
</Grid>
</ContentView>
</AbsoluteLayout>
xaml.cs
private async void Delete_Invoked(object sender, EventArgs e)
{
SwipeItem itemswipe = sender as SwipeItem;
string item= itemswipe.BindingContext as string;
mylist.Remove(item);
undo_label.Text = "Deleted \"" + item+ "\"";
undo_msg.IsVisible = true;
await Task.Delay(10000);
undo_msg.IsVisible = false;
}
protected override void OnAppearing()
{
base.OnAppearing();
list.ItemsSource = mylist;
}
If i delete an item from list and after 5 sec if I deletes another item, i want the current Task.Delay to be cancelled and start a new one. I heard this can done using cancellation token. But i don't know!
Help would be appreciated.
