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

Commit fa20cfe

Browse files
committed
Add command demos.
1 parent 965333c commit fa20cfe

File tree

8 files changed

+237
-2
lines changed

8 files changed

+237
-2
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:DragAndDropGesture"
5+
x:Class="DragAndDropGesture.CustomDataPackageCommandDemoPage"
6+
Title="Custom data package demo">
7+
<ContentPage.BindingContext>
8+
<local:CustomDataPackageViewModel />
9+
</ContentPage.BindingContext>
10+
<ContentPage.Resources>
11+
<local:Square x:Key="bigSquare">
12+
<x:Arguments>
13+
<x:Double>200</x:Double>
14+
<x:Double>200</x:Double>
15+
</x:Arguments>
16+
</local:Square>
17+
<local:Square x:Key="smallSquare">
18+
<x:Arguments>
19+
<x:Double>150</x:Double>
20+
<x:Double>150</x:Double>
21+
</x:Arguments>
22+
</local:Square>
23+
</ContentPage.Resources>
24+
<StackLayout Margin="20">
25+
<Label Text="Drag the square with the largest area to the grey rectangle." />
26+
<Rectangle Stroke="Red"
27+
Fill="DarkBlue"
28+
StrokeThickness="4"
29+
HeightRequest="200"
30+
WidthRequest="200"
31+
HorizontalOptions="Center">
32+
<Rectangle.GestureRecognizers>
33+
<DragGestureRecognizer CanDrag="True"
34+
DragStartingCommand="{Binding DragStartingCommand}"
35+
DragStartingCommandParameter="{StaticResource bigSquare}" />
36+
</Rectangle.GestureRecognizers>
37+
</Rectangle>
38+
<Rectangle Stroke="DarkBlue"
39+
Fill="Red"
40+
StrokeThickness="4"
41+
HeightRequest="150"
42+
WidthRequest="150"
43+
HorizontalOptions="Center">
44+
<Rectangle.GestureRecognizers>
45+
<DragGestureRecognizer CanDrag="True"
46+
DragStartingCommand="{Binding DragStartingCommand}"
47+
DragStartingCommandParameter="{StaticResource smallSquare}" />
48+
</Rectangle.GestureRecognizers>
49+
</Rectangle>
50+
<Frame CornerRadius="0"
51+
HasShadow="False"
52+
BackgroundColor="Silver"
53+
HeightRequest="200">
54+
<Frame.GestureRecognizers>
55+
<DropGestureRecognizer AllowDrop="True"
56+
DropCommand="{Binding DropCommand}"
57+
DropCommandParameter="{StaticResource bigSquare}" />
58+
</Frame.GestureRecognizers>
59+
</Frame>
60+
</StackLayout>
61+
</ContentPage>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Xamarin.Forms;
2+
3+
namespace DragAndDropGesture
4+
{
5+
public partial class CustomDataPackageCommandDemoPage : ContentPage
6+
{
7+
public CustomDataPackageCommandDemoPage()
8+
{
9+
InitializeComponent();
10+
11+
MessagingCenter.Subscribe<CustomDataPackageViewModel, string>(this, "Correct", async (s, e) =>
12+
{
13+
await DisplayAlert("Correct", e, "OK");
14+
});
15+
16+
MessagingCenter.Subscribe<CustomDataPackageViewModel, string>(this, "Incorrect", async (s, e) =>
17+
{
18+
await DisplayAlert("Incorrect", e, "OK");
19+
});
20+
}
21+
}
22+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Windows.Input;
2+
using Xamarin.Forms;
3+
4+
namespace DragAndDropGesture
5+
{
6+
public class CustomDataPackageViewModel
7+
{
8+
Square draggedSquare;
9+
10+
public ICommand DragStartingCommand => new Command<Square>(RegisterDragData);
11+
public ICommand DropCommand => new Command<Square>(ProcessDrop);
12+
13+
void RegisterDragData(Square square)
14+
{
15+
draggedSquare = square;
16+
}
17+
18+
void ProcessDrop(Square square)
19+
{
20+
if (square.Area.Equals(draggedSquare.Area))
21+
{
22+
MessagingCenter.Send<CustomDataPackageViewModel, string>(this, "Correct", "Congratulations!");
23+
}
24+
else
25+
{
26+
MessagingCenter.Send<CustomDataPackageViewModel, string>(this, "Incorrect", "Try again.");
27+
}
28+
}
29+
}
30+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:DragAndDropGesture"
5+
x:Class="DragAndDropGesture.DataPackageCommandDemoPage"
6+
Title="Data package demo">
7+
<ContentPage.BindingContext>
8+
<local:DataPackageViewModel />
9+
</ContentPage.BindingContext>
10+
<StackLayout Margin="20">
11+
<Label Text="Drag the cat to the grey rectangle." />
12+
<Image Source="monkeyface.png"
13+
HorizontalOptions="Center">
14+
<Image.GestureRecognizers>
15+
<DragGestureRecognizer CanDrag="True"
16+
DragStartingCommand="{Binding DragCommand}"
17+
DragStartingCommandParameter="Monkey" />
18+
</Image.GestureRecognizers>
19+
</Image>
20+
<Path Stroke="Black"
21+
StrokeThickness="4"
22+
RenderTransform="0.5 0 0 0.5 0 0"
23+
HorizontalOptions="Center">
24+
<Path.GestureRecognizers>
25+
<DragGestureRecognizer CanDrag="True"
26+
DragStartingCommand="{Binding DragCommand}"
27+
DragStartingCommandParameter="Cat" />
28+
</Path.GestureRecognizers>
29+
<Path.Data>
30+
<PathGeometry Figures="M 160 140 L 150 50 220 103
31+
M 320 140 L 330 50 260 103
32+
M 215 230 L 40 200
33+
M 215 240 L 40 240
34+
M 215 250 L 40 280
35+
M 265 230 L 440 200
36+
M 265 240 L 440 240
37+
M 265 250 L 440 280
38+
M 240 100
39+
A 100 100 0 0 1 240 300
40+
A 100 100 0 0 1 240 100
41+
M 180 170
42+
A 40 40 0 0 1 220 170
43+
A 40 40 0 0 1 180 170
44+
M 300 170
45+
A 40 40 0 0 1 260 170
46+
A 40 40 0 0 1 300 170" />
47+
</Path.Data>
48+
</Path>
49+
<Frame CornerRadius="0"
50+
HasShadow="False"
51+
BackgroundColor="Silver"
52+
HeightRequest="200">
53+
<Frame.GestureRecognizers>
54+
<DropGestureRecognizer AllowDrop="True"
55+
DropCommand="{Binding DropCommand}" />
56+
</Frame.GestureRecognizers>
57+
</Frame>
58+
</StackLayout>
59+
</ContentPage>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Xamarin.Forms;
2+
3+
namespace DragAndDropGesture
4+
{
5+
public partial class DataPackageCommandDemoPage : ContentPage
6+
{
7+
public DataPackageCommandDemoPage()
8+
{
9+
InitializeComponent();
10+
11+
MessagingCenter.Subscribe<DataPackageViewModel, string>(this, "Correct", async (s, e) =>
12+
{
13+
await DisplayAlert("Correct", e, "OK");
14+
});
15+
16+
MessagingCenter.Subscribe<DataPackageViewModel, string>(this, "Incorrect", async (s, e) =>
17+
{
18+
await DisplayAlert("Incorrect", e, "OK");
19+
});
20+
}
21+
}
22+
}

WorkingWithGestures/DragAndDropGesture/DragAndDropGesture/DataPackageDemoPage.xaml.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-

2-
using Xamarin.Forms;
1+
using Xamarin.Forms;
32

43
namespace DragAndDropGesture
54
{
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Windows.Input;
2+
using Xamarin.Forms;
3+
4+
namespace DragAndDropGesture
5+
{
6+
public class DataPackageViewModel
7+
{
8+
string dragData;
9+
10+
public ICommand DragCommand => new Command<string>(RegisterDragData);
11+
public ICommand DropCommand => new Command(ProcessDrop);
12+
13+
void RegisterDragData(string data)
14+
{
15+
dragData = data;
16+
}
17+
18+
void ProcessDrop()
19+
{
20+
if (dragData.Equals("Cat"))
21+
{
22+
MessagingCenter.Send<DataPackageViewModel, string>(this, "Correct", "Congratulations!");
23+
}
24+
else if (dragData.Equals("Monkey"))
25+
{
26+
MessagingCenter.Send<DataPackageViewModel, string>(this, "Incorrect", "Try again.");
27+
}
28+
}
29+
}
30+
}

WorkingWithGestures/DragAndDropGesture/DragAndDropGesture/MainPage.xaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,29 @@
88
<TableRoot>
99
<TableSection Title="Drag and drop">
1010
<TextCell Text="Text drag demo"
11+
Detail="Events"
1112
Command="{Binding NavigateCommand}"
1213
CommandParameter="{x:Type local:TextDragDemoPage}" />
1314
<TextCell Text="Image drag demo"
15+
Detail="Events"
1416
Command="{Binding NavigateCommand}"
1517
CommandParameter="{x:Type local:ImageDragDemoPage}" />
1618
<TextCell Text="Data package drag demo"
19+
Detail="Events"
1720
Command="{Binding NavigateCommand}"
1821
CommandParameter="{x:Type local:DataPackageDemoPage}" />
22+
<TextCell Text="Data package drag demo"
23+
Detail="Commands"
24+
Command="{Binding NavigateCommand}"
25+
CommandParameter="{x:Type local:DataPackageCommandDemoPage}" />
1926
<TextCell Text="Custom data package drag demo"
27+
Detail="Events"
2028
Command="{Binding NavigateCommand}"
2129
CommandParameter="{x:Type local:CustomDataPackageDemoPage}" />
30+
<TextCell Text="Custom data package drag demo"
31+
Detail="Commands"
32+
Command="{Binding NavigateCommand}"
33+
CommandParameter="{x:Type local:CustomDataPackageCommandDemoPage}" />
2234
</TableSection>
2335
</TableRoot>
2436
</TableView>

0 commit comments

Comments
 (0)