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)
{
e.Data.Properties["value"] = (sender as Element)?.Parent.BindingContext;
e.Data.Properties["value"] = (sender as Element)?.BindingContext;
}
private void OnDrop(object sender, DropEventArgs e)

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

@ -23,9 +23,9 @@ public partial class InCellsPage : ContentPage
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)
{
@ -36,19 +36,12 @@ public partial class InCellsPage : ContentPage
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
&& 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;
e.Data.Properties["value"] = ((sender as Element)?.BindingContext as Cage)?.MyPrecious;
if((sender as Element)?.BindingContext is Cage cage)
cage.MyPrecious = null;
}

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

Loading…
Cancel
Save