Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Fix exception deleting items from BindableLayout using SwipeView (#14068
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jsuarezruiz committed Oct 11, 2021
1 parent 3703741 commit ba7bdcd
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8" ?>
<local:TestContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:local="using:Xamarin.Forms.Controls"
x:Class="Xamarin.Forms.Controls.Issues.Issue14066"
Title="Issue 14066">
<StackLayout>
<Label
Padding="12"
BackgroundColor="Black"
TextColor="White"
Text="Swipe any SwipeView, without exceptions, the test has passed." />
<StackLayout
Margin="30"
Orientation="Vertical">
<Button
Text="Add Items to the List"
Command="{Binding AddCommand}"
TextColor="White"
BackgroundColor="Black" />
<StackLayout
x:Name="MyList"
BindableLayout.ItemsSource="{Binding CollectionsList}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<SwipeView>
<Grid
HeightRequest="40"
BackgroundColor="White"
Margin="10">
<Label
VerticalOptions="CenterAndExpand"
Text="{Binding Name}" />
</Grid>
<SwipeView.LeftItems>
<SwipeItems Mode="Execute">
<SwipeItemView
BackgroundColor="Red"
Command="{Binding Path=BindingContext.DeleteCommand, Source={x:Reference MyList}}"
CommandParameter="{Binding .}" />
</SwipeItems>
</SwipeView.LeftItems>
<SwipeView.RightItems>
<SwipeItems Mode="Execute">
<SwipeItemView
BackgroundColor="Red"
Command="{Binding Path=BindingContext.DeleteCommand, Source={x:Reference MyList}}"
CommandParameter="{Binding .}" />
</SwipeItems>
</SwipeView.RightItems>
</SwipeView>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</StackLayout>
</StackLayout>
</local:TestContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System.Collections.Generic;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System.Collections.ObjectModel;
using System.Windows.Input;

#if UITEST
using Xamarin.Forms.Core.UITests;
using Xamarin.UITest;
using NUnit.Framework;
#endif

namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 14066, "[Bug] Deleting items with SwipeView in StackLayout",
PlatformAffected.Android)]
public partial class Issue14066 : TestContentPage
{
public Issue14066()
{
#if APP
InitializeComponent();
BindingContext = new Issue14066ViewModel();
#endif
}

protected override void Init()
{

}
}

public class Issue14066Model
{
public string Name { get; set; }
}

public class Issue14066ViewModel : BindableObject
{
ObservableCollection<Issue14066Model> _collectionsList;

public ObservableCollection<Issue14066Model> CollectionsList
{
get { return _collectionsList; }
set
{
_collectionsList = value;
OnPropertyChanged();
}
}

public ICommand DeleteCommand { get; }

public ICommand AddCommand { get; }


public Issue14066ViewModel()
{
CollectionsList = new ObservableCollection<Issue14066Model>()
{
new Issue14066Model { Name= "Item 1" },
new Issue14066Model { Name= "Item 2" },
new Issue14066Model { Name= "Item 3" },
new Issue14066Model { Name= "Item 4" },
};

DeleteCommand = new Command(OnDeleteTapped);

AddCommand = new Command(AddItmes);
}

void AddItmes(object obj)
{
Issue14066Model offersModel = new Issue14066Model
{
Name = "New Item Added"
};

CollectionsList.Add(offersModel);
}

void OnDeleteTapped(object obj)
{
var content = obj as Issue14066Model;
CollectionsList.Remove(content);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1774,6 +1774,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue13616.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13670.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13684.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue14066.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue8129.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue12300.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue12150.cs" />
Expand Down Expand Up @@ -2231,6 +2232,9 @@
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue13684.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue14066.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue14433.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
Expand Down
4 changes: 2 additions & 2 deletions Xamarin.Forms.Platform.Android/Renderers/SwipeViewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ bool ProcessSwipingInteractions(MotionEvent e)
case MotionEventActions.Up:
handled = HandleTouchInteractions(GestureStatus.Completed, point);

if (Parent == null)
if (_isDisposed || Parent == null)
break;

Parent.RequestDisallowInterceptTouchEvent(false);
Expand All @@ -483,7 +483,7 @@ bool ProcessSwipingInteractions(MotionEvent e)
case MotionEventActions.Cancel:
handled = HandleTouchInteractions(GestureStatus.Canceled, point);

if (Parent == null)
if (_isDisposed || Parent == null)
break;

Parent.RequestDisallowInterceptTouchEvent(false);
Expand Down

0 comments on commit ba7bdcd

Please sign in to comment.