Added sample (#7650)
authorJavier Suárez Ruiz <javiersuarezruiz@hotmail.com>
Tue, 15 Oct 2019 08:37:57 +0000 (10:37 +0200)
committerRui Marinho <me@ruimarinho.net>
Tue, 15 Oct 2019 08:37:57 +0000 (09:37 +0100)
Xamarin.Forms.Controls/GalleryPages/CollectionViewGalleries/CarouselViewGalleries/CarouselViewGallery.cs
Xamarin.Forms.Controls/GalleryPages/CollectionViewGalleries/CarouselViewGalleries/EmptyCarouselGallery.xaml [new file with mode: 0644]
Xamarin.Forms.Controls/GalleryPages/CollectionViewGalleries/CarouselViewGalleries/EmptyCarouselGallery.xaml.cs [new file with mode: 0644]

index d446ff6..f1d6376 100644 (file)
@@ -38,7 +38,9 @@ namespace Xamarin.Forms.Controls.GalleryPages.CollectionViewGalleries.CarouselVi
                                                GalleryBuilder.NavButton("CarouselView Snap", () =>
                                                        new CarouselSnapGallery(), Navigation),
                                                GalleryBuilder.NavButton("ObservableCollection and CarouselView", () =>
-                                                       new CollectionCarouselViewGallery(), Navigation)
+                                                       new CollectionCarouselViewGallery(), Navigation),
+                                               GalleryBuilder.NavButton("CarouselView EmptyView", () =>
+                                                       new EmptyCarouselGallery(), Navigation)
                                        }
                                }
                        };
diff --git a/Xamarin.Forms.Controls/GalleryPages/CollectionViewGalleries/CarouselViewGalleries/EmptyCarouselGallery.xaml b/Xamarin.Forms.Controls/GalleryPages/CollectionViewGalleries/CarouselViewGalleries/EmptyCarouselGallery.xaml
new file mode 100644 (file)
index 0000000..76b14e0
--- /dev/null
@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ContentPage
+    xmlns="http://xamarin.com/schemas/2014/forms"
+    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
+    xmlns:gallery="clr-namespace:Xamarin.Forms.Controls.GalleryPages.CollectionViewGalleries.CarouselViewGalleries"
+    x:Class="Xamarin.Forms.Controls.GalleryPages.CollectionViewGalleries.CarouselViewGalleries.EmptyCarouselGallery"
+    Title="Carousel EmptyView">
+    <ContentPage.Content>
+        <Grid>
+            <Grid.RowDefinitions>
+                <RowDefinition Height="Auto" />
+                <RowDefinition Height="*" />
+            </Grid.RowDefinitions>
+            <StackLayout
+                Grid.Row="0"
+                Orientation="Horizontal">
+                <Button
+                    Text="Add Items"
+                    Command="{Binding AddCommand}"/>
+                <Button
+                    Text="Clear Items"
+                    Command="{Binding ClearCommand}"/>
+            </StackLayout>
+            <CarouselView
+                Grid.Row="1"
+                AutomationId="TheCarouselView"
+                x:Name="Carousel"
+                ItemsSource="{Binding Items}"
+                Position="{Binding Position}">
+            <CarouselView.ItemTemplate>
+                <DataTemplate>
+                   <Grid
+                       BackgroundColor="{Binding Color}">
+                       <Label
+                           HorizontalOptions="Center"
+                           VerticalOptions="Center"
+                           Text="{Binding Name}" />
+                   </Grid>
+                </DataTemplate>
+            </CarouselView.ItemTemplate>
+                <CarouselView.EmptyView>
+                    <Grid
+                        BackgroundColor="YellowGreen">
+                        <Label
+                            HorizontalOptions="Center"
+                            VerticalOptions="Center"
+                            Text="This is the EmptyView" />
+                    </Grid>
+                </CarouselView.EmptyView>
+        </CarouselView>
+        </Grid>
+    </ContentPage.Content>
+</ContentPage>
\ No newline at end of file
diff --git a/Xamarin.Forms.Controls/GalleryPages/CollectionViewGalleries/CarouselViewGalleries/EmptyCarouselGallery.xaml.cs b/Xamarin.Forms.Controls/GalleryPages/CollectionViewGalleries/CarouselViewGalleries/EmptyCarouselGallery.xaml.cs
new file mode 100644 (file)
index 0000000..9d1f743
--- /dev/null
@@ -0,0 +1,85 @@
+using System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Windows.Input;
+using Xamarin.Forms.Internals;
+
+namespace Xamarin.Forms.Controls.GalleryPages.CollectionViewGalleries.CarouselViewGalleries
+{
+       [Preserve(AllMembers = true)]
+       public partial class EmptyCarouselGallery : ContentPage
+       {
+               public EmptyCarouselGallery()
+               {
+                       InitializeComponent();
+                       BindingContext = new EmptyCarouselGalleryViewModel();
+               }
+       }
+
+       [Preserve(AllMembers = true)]
+       public class EmptyCarouselGalleryViewModel : BindableObject
+       {
+               ObservableCollection<CarouselData> _items;
+
+               public EmptyCarouselGalleryViewModel()
+               {
+                       Items = new ObservableCollection<CarouselData>();
+               }
+
+               public ObservableCollection<CarouselData> Items
+               {
+                       get { return _items; }
+                       set
+                       {
+                               _items = value;
+                               OnPropertyChanged();
+                       }
+               }
+
+               public ICommand AddCommand => new Command(Add);
+
+               public ICommand ClearCommand => new Command(Clear);
+
+               void LoadItems()
+               {
+                       var random = new Random();
+
+                       if (Device.RuntimePlatform == Device.iOS)
+                       {
+                               var items = new List<CarouselData>();
+
+                               for (int n = 0; n < 5; n++)
+                               {
+                                       items.Add(new CarouselData
+                                       {
+                                               Color = Color.FromRgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)),
+                                               Name = $"{n + 1}"
+                                       });
+                               }
+
+                               Items = new ObservableCollection<CarouselData>(items);
+                       }
+                       else
+                       {
+                               for (int n = 0; n < 5; n++)
+                               {
+                                       Items.Add(new CarouselData
+                                       {
+                                               Color = Color.FromRgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)),
+                                               Name = $"{n + 1}"
+                                       });
+                               }
+                       }
+               }
+
+               void Add()
+               {
+                       LoadItems();
+               }
+
+               void Clear()
+               {
+                       Items?.Clear();
+               }
+       }
+}
\ No newline at end of file