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

Commit 28b9aa2

Browse files
committed
[Controls] Add test sample for #13296
1 parent 3276a59 commit 28b9aa2

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

Xamarin.Forms.Controls/GalleryPages/CollectionViewGalleries/CarouselViewGalleries/CarouselXamlGallery.xaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<RowDefinition Height="Auto" />
1313
<RowDefinition Height="Auto" />
1414
<RowDefinition Height="Auto" />
15+
<RowDefinition Height="Auto" />
1516
</Grid.RowDefinitions>
1617
<Grid.ColumnDefinitions>
1718
<ColumnDefinition />
@@ -29,9 +30,11 @@
2930
<Button Command="{Binding RemoveCommand}" AutomationId="btnRemove" Text="{Binding Path=Selected.Index, StringFormat='Remove {0}'}" BackgroundColor="LightGray" TextColor="Black" />
3031
<Button Command="{Binding NextCommand}" AutomationId="btnNext" Text="&gt;" FontAttributes="Bold" BackgroundColor="LightGray" TextColor="Black" />
3132
</StackLayout>
33+
<Button Grid.Row="5" Text="Clear" Command="{Binding ClearCommand}" AutomationId="btnClear"/>
34+
<Button Grid.Row="5" Grid.Column="1" Text="Set" Command="{Binding SetCommand}" AutomationId="btnSet"/>
3235
<CarouselView
3336
x:Name="carousel"
34-
Grid.Row="5" Grid.ColumnSpan="2"
37+
Grid.Row="6" Grid.ColumnSpan="2"
3538
Loop="{Binding IsLoop}"
3639
AutomationId="TheCarouselView"
3740
ItemsSource="{Binding Items}"

Xamarin.Forms.Controls/GalleryPages/CollectionViewGalleries/CarouselViewGalleries/CarouselXamlGallery.xaml.cs

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,7 @@ public CarouselViewModel(CarouselXamlSampleType type, bool loop, int initialItem
3535
IsLoop = loop;
3636
_type = type;
3737

38-
var items = new List<CarouselItem>();
39-
for (int i = 0; i < initialItems; i++)
40-
{
41-
switch (_type)
42-
{
43-
case CarouselXamlSampleType.Peek:
44-
items.Add(new CarouselItem(i, "cardBackground.png"));
45-
break;
46-
default:
47-
items.Add(new CarouselItem(i));
48-
break;
49-
}
50-
}
51-
52-
MessagingCenter.Subscribe<ExampleTemplateCarousel>(this, "remove", (obj) => Items.Remove(obj.BindingContext as CarouselItem));
53-
54-
Items = new ObservableCollection<CarouselItem>(items);
55-
Count = Items.Count - 1;
56-
57-
if (startCurrentItem != -1)
58-
Selected = Items[startCurrentItem];
38+
SetItems(initialItems, startCurrentItem);
5939
}
6040

6141
public bool IsLoop
@@ -124,6 +104,44 @@ public CarouselItem Selected
124104
Selected = newItem;
125105
}
126106
});
107+
108+
public ICommand ClearCommand => new Command(ClearItems);
109+
110+
public ICommand SetCommand => new Command(() =>
111+
{
112+
SetItems(10, 5);
113+
});
114+
115+
void ClearItems()
116+
{
117+
Items.Clear();
118+
Selected = null;
119+
}
120+
121+
void SetItems(int initialItems, int startCurrentItem)
122+
{
123+
var items = new List<CarouselItem>();
124+
for (int i = 0; i < initialItems; i++)
125+
{
126+
switch (_type)
127+
{
128+
case CarouselXamlSampleType.Peek:
129+
items.Add(new CarouselItem(i, "cardBackground.png"));
130+
break;
131+
default:
132+
items.Add(new CarouselItem(i));
133+
break;
134+
}
135+
}
136+
137+
MessagingCenter.Subscribe<ExampleTemplateCarousel>(this, "remove", (obj) => Items.Remove(obj.BindingContext as CarouselItem));
138+
139+
Items = new ObservableCollection<CarouselItem>(items);
140+
Count = Items.Count - 1;
141+
142+
if (startCurrentItem != -1)
143+
Selected = Items[startCurrentItem];
144+
}
127145
}
128146

129147
[Preserve(AllMembers = true)]

0 commit comments

Comments
 (0)