updated drag and drop sample

master
Marc CHEVALDONNE 2 weeks ago
parent 85ada305bd
commit 9538d4781f

@ -24,7 +24,7 @@ public partial class ExchangeNounoursPage : ContentPage
private void OnDragStarting(object sender, DragStartingEventArgs e) private void OnDragStarting(object sender, DragStartingEventArgs e)
{ {
e.Data.Properties["value"] = (sender as Element)?.Parent.BindingContext; e.Data.Properties["value"] = (sender as Element)?.BindingContext;
} }
private void OnDrop(object sender, DropEventArgs e) private void OnDrop(object sender, DropEventArgs e)

@ -66,8 +66,8 @@
Radius="10" Opacity="0.8"/> Radius="10" Opacity="0.8"/>
</Border.Shadow> </Border.Shadow>
<Border.GestureRecognizers> <Border.GestureRecognizers>
<DragGestureRecognizer DragStarting="DragGestureRecognizer_DragStarting1"/> <DragGestureRecognizer DragStarting="Cages_DragStarting"/>
<DropGestureRecognizer Drop="DropGestureRecognizer_Drop"/> <DropGestureRecognizer Drop="Cages_Drop"/>
</Border.GestureRecognizers> </Border.GestureRecognizers>
<Image Source="{Binding MyPrecious.Image}"/> <Image Source="{Binding MyPrecious.Image}"/>
</Border> </Border>

@ -23,9 +23,9 @@ public partial class InCellsPage : ContentPage
BindingContext = this; BindingContext = this;
} }
private void DropGestureRecognizer_Drop(object sender, DropEventArgs e) private void Cages_Drop(object sender, DropEventArgs e)
{ {
if ((sender as Element)?.Parent.BindingContext is Cage receivingElement) if ((sender as Element)?.BindingContext is Cage receivingElement)
{ {
if (e.Data.Properties["value"] is Nounours draggedNounours) if (e.Data.Properties["value"] is Nounours draggedNounours)
{ {
@ -36,19 +36,12 @@ public partial class InCellsPage : ContentPage
private void Bag_DragStarting(object sender, DragStartingEventArgs e) private void Bag_DragStarting(object sender, DragStartingEventArgs e)
{ {
e.Data.Properties["value"] = (sender as Element)?.Parent.BindingContext; e.Data.Properties["value"] = (sender as Element)?.BindingContext;
} }
private void DropGestureRecognizer_DragLeave(object sender, DragEventArgs e) private void Cages_DragStarting(object sender, DragStartingEventArgs e)
{ {
if(e.Data.Properties["value"] is Nounours draggedNounours e.Data.Properties["value"] = ((sender as Element)?.BindingContext as Cage)?.MyPrecious;
&& sender is Cage cage)
cage.MyPrecious = null;
}
private void DragGestureRecognizer_DragStarting1(object sender, DragStartingEventArgs e)
{
e.Data.Properties["value"] = ((sender as Element)?.Parent.BindingContext as Cage)?.MyPrecious;
if((sender as Element)?.BindingContext is Cage cage) if((sender as Element)?.BindingContext is Cage cage)
cage.MyPrecious = null; cage.MyPrecious = null;
} }

@ -22,23 +22,22 @@ public partial class OrganizeNounoursPage : ContentPage
private void OnDragStarting(object sender, DragStartingEventArgs e) private void OnDragStarting(object sender, DragStartingEventArgs e)
{ {
e.Data.Properties["value"] = (sender as Element)?.Parent.BindingContext; e.Data.Properties["value"] = (sender as Element)?.BindingContext;
} }
private void OnDrop(object sender, DropEventArgs e) private void OnDrop(object sender, DropEventArgs e)
{ {
var receivingElement = (sender as Element)?.Parent.BindingContext as Nounours; if ((sender as Element)?.BindingContext is not Nounours receivingElement) return;
if (e.Data.Properties["value"] as Nounours is not Nounours draggedElement) return;
var draggedElement = e.Data.Properties["value"] as Nounours;
int draggedIndex = NounoursCollection.IndexOf(draggedElement); int draggedIndex = NounoursCollection.IndexOf(draggedElement);
if (NounoursCollection.Contains(draggedElement)) if (draggedIndex > -1)
{ {
NounoursCollection.RemoveAt(draggedIndex); NounoursCollection.RemoveAt(draggedIndex);
} }
int receivingIndex = NounoursCollection.IndexOf(receivingElement); int receivingIndex = NounoursCollection.IndexOf(receivingElement);
if (receivingIndex == -1) throw new ArgumentNullException("There should be a receiving element");
if (receivingIndex >= draggedIndex) if (receivingIndex >= draggedIndex)
{ {

Loading…
Cancel
Save