From ee1eca51d57a985c2783f110598be3bbf5b5544f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Javier=20Su=C3=A1rez=20Ruiz?= Date: Mon, 9 Dec 2019 20:57:38 +0100 Subject: [PATCH] Added Converter to simplify the use of Layouts in the CarouselView (#8665) fixes #7813 --- .../Issue7813.xaml | 45 ++++++++++++ .../Issue7813.xaml.cs | 83 ++++++++++++++++++++++ .../Xamarin.Forms.Controls.Issues.Shared.projitems | 2 + .../Items/CarouselLayoutTypeConverter.cs | 23 ++++++ Xamarin.Forms.Core/Items/CarouselView.cs | 1 + Xamarin.Forms.Core/Items/LinearItemsLayout.cs | 6 ++ 6 files changed, 160 insertions(+) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7813.xaml create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7813.xaml.cs create mode 100644 Xamarin.Forms.Core/Items/CarouselLayoutTypeConverter.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7813.xaml b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7813.xaml new file mode 100644 index 0000000..f92876f --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7813.xaml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7813.xaml.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7813.xaml.cs new file mode 100644 index 0000000..883f683 --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7813.xaml.cs @@ -0,0 +1,83 @@ +using Xamarin.Forms.Internals; +using Xamarin.Forms.Xaml; +using System; +using System.Collections.ObjectModel; +using System.Collections.Generic; +using Xamarin.Forms.CustomAttributes; + +#if UITEST +using NUnit.Framework; +using Xamarin.UITest; +using Xamarin.Forms.Core.UITests; +#endif + +namespace Xamarin.Forms.Controls.Issues +{ +#if UITEST + [NUnit.Framework.Category(UITestCategories.CarouselView)] +#endif +#if APP + [XamlCompilation(XamlCompilationOptions.Compile)] +#endif + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Github, 7813, "CarouselView vertical layout could use a converter", PlatformAffected.All)] + public partial class Issue7813 : ContentPage + { + public Issue7813() + { +#if APP + InitializeComponent(); +#endif + BindingContext = new Issue7813ViewModel(); + } + } + + [Preserve(AllMembers = true)] + public class Issue7813Model + { + public Color Color { get; set; } + public string Name { get; set; } + } + + [Preserve(AllMembers = true)] + public class Issue7813ViewModel : BindableObject + { + ObservableCollection _items; + + public Issue7813ViewModel() + { + LoadItems(); + } + + public ObservableCollection Items + { + get { return _items; } + set + { + _items = value; + OnPropertyChanged(); + } + } + + void LoadItems() + { + Items = new ObservableCollection(); + + var random = new Random(); + var items = new List(); + + for (int n = 0; n < 5; n++) + { + items.Add(new Issue7813Model + { + Color = Color.FromRgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)), + Name = $"{n + 1}" + }); + } + + _items = new ObservableCollection(items); + OnPropertyChanged(nameof(Items)); + + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index 4618f92..6f53be4 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -1173,6 +1173,7 @@ + @@ -1311,6 +1312,7 @@ MSBuild:UpdateDesignTimeXaml + diff --git a/Xamarin.Forms.Core/Items/CarouselLayoutTypeConverter.cs b/Xamarin.Forms.Core/Items/CarouselLayoutTypeConverter.cs new file mode 100644 index 0000000..3a7a447 --- /dev/null +++ b/Xamarin.Forms.Core/Items/CarouselLayoutTypeConverter.cs @@ -0,0 +1,23 @@ +using System; + +namespace Xamarin.Forms +{ + [Xaml.TypeConversion(typeof(LinearItemsLayout))] + public class CarouselLayoutTypeConverter : TypeConverter + { + public override object ConvertFromInvariantString(string value) + { + if (value == "HorizontalList") + { + return LinearItemsLayout.CarouselDefault; + } + + if (value == "VerticalList") + { + return LinearItemsLayout.CarouselVertical; + } + + throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(IItemsLayout)}"); + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Core/Items/CarouselView.cs b/Xamarin.Forms.Core/Items/CarouselView.cs index be4c892..770cbcf 100644 --- a/Xamarin.Forms.Core/Items/CarouselView.cs +++ b/Xamarin.Forms.Core/Items/CarouselView.cs @@ -151,6 +151,7 @@ namespace Xamarin.Forms BindableProperty.Create(nameof(ItemsLayout), typeof(LinearItemsLayout), typeof(ItemsView), LinearItemsLayout.CarouselDefault); + [TypeConverter(typeof(CarouselLayoutTypeConverter))] public LinearItemsLayout ItemsLayout { get => (LinearItemsLayout)GetValue(ItemsLayoutProperty); diff --git a/Xamarin.Forms.Core/Items/LinearItemsLayout.cs b/Xamarin.Forms.Core/Items/LinearItemsLayout.cs index 7d46056..2a33028 100644 --- a/Xamarin.Forms.Core/Items/LinearItemsLayout.cs +++ b/Xamarin.Forms.Core/Items/LinearItemsLayout.cs @@ -11,6 +11,12 @@ namespace Xamarin.Forms public static readonly IItemsLayout Vertical = new LinearItemsLayout(ItemsLayoutOrientation.Vertical); public static readonly IItemsLayout Horizontal = new LinearItemsLayout(ItemsLayoutOrientation.Horizontal); + public static readonly IItemsLayout CarouselVertical = new LinearItemsLayout(ItemsLayoutOrientation.Vertical) + { + SnapPointsType = SnapPointsType.MandatorySingle, + SnapPointsAlignment = SnapPointsAlignment.Center + }; + internal static readonly LinearItemsLayout CarouselDefault = new LinearItemsLayout(ItemsLayoutOrientation.Horizontal) { SnapPointsType = SnapPointsType.MandatorySingle, -- 2.7.4