From 2f9aff82cacaf694819f9a77c501c3741703f381 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Javier=20Su=C3=A1rez=20Ruiz?= Date: Mon, 16 Sep 2019 14:13:07 +0200 Subject: [PATCH] Added Carousel SnapBehavior Core sample (#7444) --- .../CarouselViewGalleries/CarouselSnapGallery.cs | 141 +++++++++++++++++++++ .../CarouselViewGalleries/CarouselViewGallery.cs | 2 + 2 files changed, 143 insertions(+) create mode 100644 Xamarin.Forms.Controls/GalleryPages/CollectionViewGalleries/CarouselViewGalleries/CarouselSnapGallery.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 index 0000000..a12c126 --- /dev/null +++ b/Xamarin.Forms.Controls/GalleryPages/CollectionViewGalleries/CarouselViewGalleries/CarouselSnapGallery.cs @@ -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().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 diff --git a/Xamarin.Forms.Controls/GalleryPages/CollectionViewGalleries/CarouselViewGalleries/CarouselViewGallery.cs b/Xamarin.Forms.Controls/GalleryPages/CollectionViewGalleries/CarouselViewGalleries/CarouselViewGallery.cs index 30eec7d..6ce4b1e 100644 --- a/Xamarin.Forms.Controls/GalleryPages/CollectionViewGalleries/CarouselViewGalleries/CarouselViewGallery.cs +++ b/Xamarin.Forms.Controls/GalleryPages/CollectionViewGalleries/CarouselViewGalleries/CarouselViewGallery.cs @@ -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) } } }; -- 2.7.4