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

Commit bc51d24

Browse files
authored
Drag and drop (#11537)
* Drag and Drop Gesture Recognizers * - wire up gallery * - fix android completed * - wire up completed ios * - fix macos build * - add exclusion * - add commands, tests, and additional galleryies * - create image for ios drag * - fix macos build issue and add flag * - set flag on unit tests * Update Xamarin.Forms.Controls/GalleryPages/DragAndDropGalleries/EnablingAndDisablingGestureTests.cs Co-authored-by: Samantha Houts <samhouts@users.noreply.github.com> * - fix null check * - ios null check * - fix uwp check * - fix sample Co-authored-by: Samantha Houts <samhouts@users.noreply.github.com> fixes #10778
1 parent fa74c73 commit bc51d24

29 files changed

+1805
-32
lines changed

Xamarin.Forms.Controls/CoreGallery.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Xamarin.Forms.Controls.GalleryPages.RadioButtonGalleries;
2121
using Xamarin.Forms.Controls.GalleryPages.ShapesGalleries;
2222
using Xamarin.Forms.Controls.GalleryPages.GradientGalleries;
23+
using Xamarin.Forms.Controls.GalleryPages.DragAndDropGalleries;
2324

2425
namespace Xamarin.Forms.Controls
2526
{
@@ -344,6 +345,7 @@ public override string ToString()
344345
new GalleryPageFactory(() => new ButtonBorderBackgroundGalleryPage(VisualMarker.Material), "Button Border & Background Gallery (Material)"),
345346
new GalleryPageFactory(() => new CheckBoxCoreGalleryPage(), "CheckBox Gallery"),
346347
new GalleryPageFactory(() => new DatePickerCoreGalleryPage(), "DatePicker Gallery"),
348+
new GalleryPageFactory(() => new DragAndDropGallery(), "Drag and Drop Gallery"),
347349
new GalleryPageFactory(() => new EditorCoreGalleryPage(), "Editor Gallery"),
348350
new GalleryPageFactory(() => new FrameCoreGalleryPage(), "Frame Gallery"),
349351
new GalleryPageFactory(() => new GradientsGallery(), "Brushes Gallery"),
@@ -499,7 +501,13 @@ public CorePageView(Page rootPage, NavigationBehavior navigationBehavior = Navig
499501
var item = args.SelectedItem;
500502
var page = item as GalleryPageFactory;
501503
if (page != null)
502-
await PushPage(page.Realize());
504+
{
505+
var realize = page.Realize();
506+
if (realize is Shell)
507+
Application.Current.MainPage = realize;
508+
else
509+
await PushPage(realize);
510+
}
503511

504512
SelectedItem = null;
505513
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Collections.Generic;
2+
using Xamarin.Forms.Internals;
3+
4+
namespace Xamarin.Forms.Controls.GalleryPages.DragAndDropGalleries
5+
{
6+
[Preserve(AllMembers = true)]
7+
public class DragAndDropGallery : Shell
8+
{
9+
public DragAndDropGallery()
10+
{
11+
Device.SetFlags(new List<string> { ExperimentalFlags.DragAndDropExperimental, ExperimentalFlags.ShellUWPExperimental });
12+
Items.Add(new EnablingAndDisablingGestureTests());
13+
Items.Add(new VariousDragAndDropPermutations());
14+
}
15+
}
16+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
using System.Collections.Generic;
2+
using System.Collections.ObjectModel;
3+
using System.Linq;
4+
using System.Text;
5+
using Xamarin.Forms.Internals;
6+
7+
namespace Xamarin.Forms.Controls.GalleryPages.DragAndDropGalleries
8+
{
9+
10+
11+
[Preserve(AllMembers = true)]
12+
public class EnablingAndDisablingGestureTests : ContentPage
13+
{
14+
public EnablingAndDisablingGestureTests()
15+
{
16+
Title = "Enabling and Disabling Gestures";
17+
StackLayout stackLayout = new StackLayout();
18+
CollectionView collectionView = new CollectionView();
19+
collectionView.ItemsUpdatingScrollMode = ItemsUpdatingScrollMode.KeepScrollOffset;
20+
ObservableCollection<string> observableCollection = new ObservableCollection<string>();
21+
collectionView.ItemsSource = observableCollection;
22+
23+
Image imageSource = new Image()
24+
{
25+
Source = "coffee.png",
26+
BackgroundColor = Color.Green
27+
};
28+
29+
Image imageDestination = new Image()
30+
{
31+
BackgroundColor = Color.Purple,
32+
HeightRequest = 50,
33+
WidthRequest = 50
34+
};
35+
36+
Button addRemoveDragGesture = new Button()
37+
{
38+
Text = "Add/Remove Drag Gesture",
39+
Command = new Command(() =>
40+
{
41+
var dragGestureRecognizer = imageSource.GestureRecognizers.OfType<DragGestureRecognizer>()
42+
.FirstOrDefault();
43+
44+
if (dragGestureRecognizer != null)
45+
imageSource.GestureRecognizers.Remove(dragGestureRecognizer);
46+
else
47+
{
48+
var dragGesture = new DragGestureRecognizer()
49+
{
50+
CanDrag = true
51+
};
52+
53+
dragGesture.DragStarting += (_, args) =>
54+
{
55+
observableCollection.Insert(0, $"DragStarting");
56+
};
57+
58+
dragGesture.DropCompleted += (_, args) =>
59+
{
60+
observableCollection.Insert(0, $"DropCompleted");
61+
};
62+
63+
imageSource.GestureRecognizers.Add(dragGesture);
64+
}
65+
})
66+
};
67+
68+
Button toggleCanDrag = new Button()
69+
{
70+
Text = "Toggle Can Drag",
71+
Command = new Command(() =>
72+
{
73+
var dragGestureRecognizer = imageSource.GestureRecognizers.OfType<DragGestureRecognizer>()
74+
.FirstOrDefault();
75+
76+
if (dragGestureRecognizer != null)
77+
dragGestureRecognizer.CanDrag = !dragGestureRecognizer.CanDrag;
78+
})
79+
};
80+
81+
82+
83+
Button addRemoveDropGesture = new Button()
84+
{
85+
Text = "Add/Remove Drop Gesture",
86+
Command = new Command(() =>
87+
{
88+
var dropGestureRecognizer = imageDestination.GestureRecognizers.OfType<DropGestureRecognizer>()
89+
.FirstOrDefault();
90+
91+
if (dropGestureRecognizer != null)
92+
imageDestination.GestureRecognizers.Remove(dropGestureRecognizer);
93+
else
94+
{
95+
var dropGesture = new DropGestureRecognizer()
96+
{
97+
AllowDrop = true
98+
};
99+
100+
dropGesture.Drop += (_, args) =>
101+
{
102+
observableCollection.Insert(0, $"Drop");
103+
};
104+
105+
dropGesture.DragOver += (_, args) =>
106+
{
107+
observableCollection.Insert(0, $"DragOver");
108+
args.AcceptedOperation = DataPackageOperation.Copy;
109+
};
110+
111+
imageDestination.GestureRecognizers.Add(dropGesture);
112+
}
113+
})
114+
};
115+
116+
Button toggleCanDrop = new Button()
117+
{
118+
Text = "Toggle Can Drop",
119+
Command = new Command(() =>
120+
{
121+
var dropGestureRecognizer = imageDestination.GestureRecognizers.OfType<DropGestureRecognizer>()
122+
.FirstOrDefault();
123+
124+
if (dropGestureRecognizer != null)
125+
dropGestureRecognizer.AllowDrop = !dropGestureRecognizer.AllowDrop;
126+
})
127+
};
128+
129+
stackLayout.Children.Add(imageSource);
130+
stackLayout.Children.Add(addRemoveDragGesture);
131+
stackLayout.Children.Add(toggleCanDrag);
132+
133+
stackLayout.Children.Add(imageDestination);
134+
stackLayout.Children.Add(addRemoveDropGesture);
135+
stackLayout.Children.Add(toggleCanDrop);
136+
137+
stackLayout.Children.Add(collectionView);
138+
Content = stackLayout;
139+
}
140+
}
141+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using System;
2+
using Xamarin.Forms.Internals;
3+
using Xamarin.Forms.Markup;
4+
5+
namespace Xamarin.Forms.Controls.GalleryPages.DragAndDropGalleries
6+
{
7+
[Preserve(AllMembers = true)]
8+
public class VariousDragAndDropPermutations : ContentPage
9+
{
10+
public Color DraggingColor { get; set; }
11+
12+
public VariousDragAndDropPermutations()
13+
{
14+
Title = "Various Drag And Drop Permutations";
15+
16+
StackLayout stackLayout = new StackLayout();
17+
18+
stackLayout.Children.Add(CreateControls<Label>((drag, drop) =>
19+
{
20+
drag.Text = "Drag";
21+
drag.FontSize = 18;
22+
drop.Text = "Drop";
23+
drop.FontSize = 18;
24+
}));
25+
26+
stackLayout.Children.Add(CreateControls<Image>((drag, drop) =>
27+
{
28+
drag.HeightRequest = 50;
29+
drag.BackgroundColor = Color.Green;
30+
}));
31+
32+
stackLayout.Children.Add(CreateControls<Image>((drag, drop) =>
33+
{
34+
drag.Source = "coffee.png";
35+
drag.BackgroundColor = Color.Green;
36+
}));
37+
38+
stackLayout.Children.Add(CreateControls<Entry>(dragElementText: "Some text"));
39+
stackLayout.Children.Add(CreateControls<Editor>(dragElementText: "True"));
40+
stackLayout.Children.Add(CreateControls<DatePicker>(dragElementText: "False"));
41+
stackLayout.Children.Add(CreateControls<TimePicker>(dragElementText: $"{DateTime.Now}"));
42+
stackLayout.Children.Add(CreateControls<CheckBox>(dragElementText: $"{DateTime.Now.TimeOfDay}"));
43+
stackLayout.Children.Add(CreateControls<Entry>(dragElementText: "https://github.com/xamarin/Xamarin.Forms/blob/f27f5a3650f37894d4a1ac925d6fab4dc7350087/Xamarin.Forms.ControlGallery.Android/Resources/drawable/oasis.jpg?raw=true"));
44+
stackLayout.Children.Add(CreateControls<StackDrag>());
45+
46+
Content = stackLayout;
47+
}
48+
49+
[Preserve(AllMembers = true)]
50+
class StackDrag : StackLayout
51+
{
52+
public StackDrag()
53+
{
54+
Children.Add(new Image() { Source = "coffee.png" });
55+
Children.Add(new Label { Text = "COFFEE" });
56+
}
57+
}
58+
59+
View CreateControls<TView>(Action<TView, TView> action = null, string dragElementText = null)
60+
where TView : View
61+
{
62+
Grid layout = new Grid();
63+
64+
layout.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
65+
layout.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
66+
67+
View drag = null;
68+
69+
if (!String.IsNullOrWhiteSpace(dragElementText))
70+
drag = AddDragGesture((View)new Label() { Text = dragElementText });
71+
else
72+
drag = AddDragGesture(Activator.CreateInstance<TView>());
73+
74+
var drop = AddDropGesture(Activator.CreateInstance<TView>());
75+
76+
drop.SetBinding(VisualElement.BackgroundColorProperty, "DraggingColor");
77+
drop.BindingContext = this;
78+
layout.AddChild(drag, 0, 0);
79+
layout.AddChild(drop, 1, 0);
80+
action?.Invoke((TView)layout.Children[0], (TView)layout.Children[1]);
81+
return layout;
82+
}
83+
84+
TView AddDragGesture<TView>(TView view)
85+
where TView : View
86+
{
87+
var dragRecognizer = new DragGestureRecognizer()
88+
{
89+
CanDrag = true
90+
};
91+
92+
dragRecognizer.DragStarting += (_, args) =>
93+
{
94+
DraggingColor = Color.Purple;
95+
OnPropertyChanged(nameof(DraggingColor));
96+
97+
if(view is StackDrag sd)
98+
{
99+
args.Data.Image = "coffee.png";
100+
}
101+
};
102+
103+
dragRecognizer.DropCompleted += (_, __) =>
104+
{
105+
DraggingColor = Color.Default;
106+
OnPropertyChanged(nameof(DraggingColor));
107+
};
108+
109+
view.GestureRecognizers.Add(dragRecognizer);
110+
111+
return view;
112+
}
113+
114+
TView AddDropGesture<TView>(TView view)
115+
where TView : View
116+
{
117+
var dropRecognizer = new DropGestureRecognizer()
118+
{
119+
AllowDrop = true
120+
};
121+
122+
view.GestureRecognizers.Add(dropRecognizer);
123+
124+
return view;
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)