예제 #1
0
        public CarouselItemsGallery()
        {
            var viewModel = new CarouselItemsGalleryViewModel();

            Title = $"CarouselView (Indicators)";

            var grid = new Grid
            {
                RowDefinitions = new RowDefinitionCollection
                {
                    new RowDefinition {
                        Height = GridLength.Star
                    },
                    new RowDefinition {
                        Height = GridLength.Auto
                    }
                }
            };

            var itemsLayout =
                new LinearItemsLayout(ItemsLayoutOrientation.Horizontal)
            {
                SnapPointsType      = SnapPointsType.MandatorySingle,
                SnapPointsAlignment = SnapPointsAlignment.Center
            };

            var itemTemplate = GetCarouselTemplate();

            var carouselView = new CarouselView
            {
                ItemsLayout      = itemsLayout,
                ItemTemplate     = itemTemplate,
                ItemsSource      = viewModel.Items,
                IsScrollAnimated = true,
                IsBounceEnabled  = true,
                EmptyView        = "This is the empty view",
                PeekAreaInsets   = new Thickness(50)
            };

            var absolute = new AbsoluteLayout();

            absolute.Children.Add(carouselView, new Rectangle(0, 0, 1, 1), AbsoluteLayoutFlags.All);

            var indicators = new IndicatorView
            {
                Margin                 = new Thickness(15, 20),
                IndicatorColor         = Color.Gray,
                SelectedIndicatorColor = Color.Black,
                IndicatorsShape        = IndicatorShape.Square
            };

            //carouselView.IndicatorView = indicators;

            absolute.Children.Add(indicators, new Rectangle(.5, 1, -1, -1), AbsoluteLayoutFlags.PositionProportional);

            grid.Children.Add(absolute, 0, 0);

            var stacklayoutButtons = new StackLayout
            {
                Orientation = StackOrientation.Horizontal
            };

            var addItemButton = new Button
            {
                Text = "Add Item"
            };

            addItemButton.Clicked += (sender, e) =>
            {
                viewModel.Items.Add(new CarouselData
                {
                    Color = Color.Red,
                    Name  = $"{viewModel.Items.Count + 1}"
                });
                carouselView.Position = viewModel.Items.Count - 1;
            };

            var removeItemButton = new Button
            {
                Text = "Remove Item"
            };

            removeItemButton.Clicked += (sender, e) =>
            {
                if (viewModel.Items.Any())
                {
                    viewModel.Items.RemoveAt(viewModel.Items.Count - 1);
                }

                if (viewModel.Items.Count > 0)
                {
                    carouselView.Position = viewModel.Items.Count - 1;
                }
            };

            var clearItemsButton = new Button
            {
                Text = "Clear Items"
            };

            clearItemsButton.Clicked += (sender, e) =>
            {
                viewModel.Items.Clear();
            };

            stacklayoutButtons.Children.Add(addItemButton);
            stacklayoutButtons.Children.Add(removeItemButton);
            stacklayoutButtons.Children.Add(clearItemsButton);

            grid.Children.Add(stacklayoutButtons, 0, 1);

            Content        = grid;
            BindingContext = viewModel;
        }
        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 Xamarin.Forms.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 Xamarin.Forms.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
            {
                ItemsLayout     = itemsLayout,
                ItemTemplate    = itemTemplate,
                BackgroundColor = Color.LightGray,
                PeekAreaInsets  = new Thickness(0, 0, 100, 0),
                Margin          = new Thickness(12),
                AutomationId    = "TheCarouselView"
            };

            carouselView.SetBinding(CarouselView.ItemsSourceProperty, "Items");

            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;
        }