Added Carousel SnapBehavior Core sample (#7444)
authorJavier Suárez Ruiz <javiersuarezruiz@hotmail.com>
Mon, 16 Sep 2019 12:13:07 +0000 (14:13 +0200)
committerRui Marinho <me@ruimarinho.net>
Mon, 16 Sep 2019 12:13:07 +0000 (13:13 +0100)
Xamarin.Forms.Controls/GalleryPages/CollectionViewGalleries/CarouselViewGalleries/CarouselSnapGallery.cs [new file with mode: 0644]
Xamarin.Forms.Controls/GalleryPages/CollectionViewGalleries/CarouselViewGalleries/CarouselViewGallery.cs

diff --git a/Xamarin.Forms.Controls/GalleryPages/CollectionViewGalleries/CarouselViewGalleries/CarouselSnapGallery.cs b/Xamarin.Forms.Controls/GalleryPages/CollectionViewGalleries/CarouselViewGalleries/CarouselSnapGallery.cs
new file mode 100644 (file)
index 0000000..a12c126
--- /dev/null
@@ -0,0 +1,141 @@
+using System;
+using System.Linq;
+using Xamarin.Forms.Internals;
+using Xamarin.Forms.PlatformConfiguration;
+using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
+
+namespace Xamarin.Forms.Controls.GalleryPages.CollectionViewGalleries.CarouselViewGalleries
+{
+       [Preserve(AllMembers = true)]
+       public class CarouselSnapGallery : ContentPage
+       {
+               public CarouselSnapGallery()
+               {
+                       On<iOS>().SetLargeTitleDisplay(LargeTitleDisplayMode.Never);
+
+                       var viewModel = new CarouselItemsGalleryViewModel();
+
+                       Title = $"CarouselView Snap Options";
+
+                       var layout = new Grid
+                       {
+                               RowDefinitions = new RowDefinitionCollection
+                               {
+                                       new RowDefinition { Height = GridLength.Auto },
+                                       new RowDefinition { Height = GridLength.Auto },
+                                       new RowDefinition { Height = GridLength.Star }
+                               }
+                       };
+
+                       var snapPointsStack = new StackLayout
+                       {
+                               Margin = new Thickness(12)
+                       };
+
+                       var snapPointsLabel = new Label { FontSize = 10, Text = "SnapPointsType:" };
+                       var snapPointsTypes = Enum.GetNames(typeof(SnapPointsType)).Select(b => b).ToList();
+
+                       var snapPointsTypePicker = new Picker
+                       {
+                               ItemsSource = snapPointsTypes,
+                               SelectedItem = snapPointsTypes[1]
+                       };
+
+                       snapPointsStack.Children.Add(snapPointsLabel);
+                       snapPointsStack.Children.Add(snapPointsTypePicker);
+
+                       layout.Children.Add(snapPointsStack, 0, 0);
+
+                       var snapPointsAlignmentsStack = new StackLayout
+                       {
+                               Margin = new Thickness(12)
+                       };
+
+                       var snapPointsAlignmentsLabel = new Label { FontSize = 10, Text = "SnapPointsAlignment:" };
+                       var snapPointsAlignments = Enum.GetNames(typeof(SnapPointsAlignment)).Select(b => b).ToList();
+
+                       var snapPointsAlignmentPicker = new Picker
+                       {
+                               ItemsSource = snapPointsAlignments,
+                               SelectedItem = snapPointsAlignments[0]
+                       };
+
+                       snapPointsAlignmentsStack.Children.Add(snapPointsAlignmentsLabel);
+                       snapPointsAlignmentsStack.Children.Add(snapPointsAlignmentPicker);
+
+                       layout.Children.Add(snapPointsAlignmentsStack, 0, 1);
+
+                       var itemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Horizontal)
+                       {
+                               SnapPointsType = SnapPointsType.Mandatory,
+                               SnapPointsAlignment = SnapPointsAlignment.Start
+                       };
+
+                       var itemTemplate = GetCarouselTemplate();
+
+                       var carouselView = new CarouselView
+                       {
+                               ItemsSource = viewModel.Items,
+                               ItemsLayout = itemsLayout,
+                               ItemTemplate = itemTemplate,
+                               BackgroundColor = Color.LightGray,
+                               PeekAreaInsets = new Thickness(0, 0, 300, 0),
+                               Margin = new Thickness(12),
+                               AutomationId = "TheCarouselView"
+                       };
+
+                       layout.Children.Add(carouselView, 0, 2);
+
+
+                       snapPointsTypePicker.SelectedIndexChanged += (sender, e) =>
+                       {
+                               if (carouselView.ItemsLayout is LinearItemsLayout linearItemsLayout)
+                               {
+                                       Enum.TryParse(snapPointsTypePicker.SelectedItem.ToString(), out SnapPointsType snapPointsType);
+                                       linearItemsLayout.SnapPointsType = snapPointsType;
+                               }
+                       };
+
+                       snapPointsAlignmentPicker.SelectedIndexChanged += (sender, e) =>
+                       {
+                               if (carouselView.ItemsLayout is LinearItemsLayout linearItemsLayout)
+                               {
+                                       Enum.TryParse(snapPointsAlignmentPicker.SelectedItem.ToString(), out SnapPointsAlignment snapPointsAlignment);
+                                       linearItemsLayout.SnapPointsAlignment = snapPointsAlignment;
+                               }
+                       };
+
+                       Content = layout;
+                       BindingContext = viewModel;
+               }
+
+               internal DataTemplate GetCarouselTemplate()
+               {
+                       return new DataTemplate(() =>
+                       {
+                               var grid = new Grid();
+
+                               var info = new Label
+                               {
+                                       HorizontalOptions = LayoutOptions.Center,
+                                       VerticalOptions = LayoutOptions.Center,
+                                       Margin = new Thickness(6)
+                               };
+
+                               info.SetBinding(Label.TextProperty, new Binding("Name"));
+
+                               grid.Children.Add(info);
+
+                               var frame = new Frame
+                               {
+                                       Content = grid,
+                                       HasShadow = false
+                               };
+
+                               frame.SetBinding(BackgroundColorProperty, new Binding("Color"));
+
+                               return frame;
+                       });
+               }
+       }
+}
\ No newline at end of file
index 30eec7db3fbf50e187bbdb271f81d94e48fe1b47..6ce4b1e903987d5f33f82c0358c4d64fe8b14c44 100644 (file)
@@ -28,6 +28,8 @@ namespace Xamarin.Forms.Controls.GalleryPages.CollectionViewGalleries.CarouselVi
                                                        new CarouselXamlGallery(), Navigation),
                                                GalleryBuilder.NavButton("CarouselView (Items)", () =>
                                                        new CarouselItemsGallery(), Navigation),
+                                               GalleryBuilder.NavButton("CarouselView Snap", () =>
+                                                       new CarouselSnapGallery(), Navigation)
                                        }
                                }
                        };