diff --git a/.editorconfig b/.editorconfig index 05ecee8..43ca161 100644 --- a/.editorconfig +++ b/.editorconfig @@ -169,4 +169,7 @@ dotnet_naming_style.static_field_style.capitalization = pascal_case dotnet_diagnostic.CS4014.severity = error # IDE0051: Remove unused private members -dotnet_diagnostic.IDE0051.severity = warning \ No newline at end of file +dotnet_diagnostic.IDE0051.severity = warning + +# CS1591: Missing XML comment for publicly visible type or member +dotnet_diagnostic.CS1591.severity = none \ No newline at end of file diff --git a/Plugin.SegmentedControl.Maui/Controls/SegmentedControl.cs b/Plugin.SegmentedControl.Maui/Controls/SegmentedControl.cs index e62f809..06c68eb 100644 --- a/Plugin.SegmentedControl.Maui/Controls/SegmentedControl.cs +++ b/Plugin.SegmentedControl.Maui/Controls/SegmentedControl.cs @@ -24,7 +24,7 @@ public SegmentedControl() public event EventHandler SelectedIndexChanged; - public static readonly BindableProperty ChildrenProperty = BindableProperty.Create( + private static readonly BindableProperty ChildrenProperty = BindableProperty.Create( nameof(Children), typeof(IList), typeof(SegmentedControl), @@ -34,7 +34,7 @@ public SegmentedControl() public IList Children { get => (IList)this.GetValue(ChildrenProperty); - set => this.SetValue(ChildrenProperty, value); + private set => this.SetValue(ChildrenProperty, value); } private static void OnChildrenPropertyChanging(BindableObject bindable, object oldValue, object newValue) @@ -67,42 +67,58 @@ public IEnumerable ItemsSource private void OnItemsSourcePropertyChanged() { - var itemsSource = this.ItemsSource; - var items = itemsSource as IList; - if (items == null && itemsSource is IEnumerable list) + List segmentedControlOptions; + + if (this.ItemsSource is IEnumerable s) { - items = list.Cast().ToList(); + segmentedControlOptions = s.ToList(); } - - if (items != null) + else { - var textValues = items as IEnumerable; - if (textValues == null && items.Count > 0 && items[0] is string) - { - textValues = items.Cast(); - } + var itemsSource = this.ItemsSource; + var items = itemsSource as IList; - if (textValues != null) + if (items == null && itemsSource is IEnumerable enumerable) { - this.Children = new List(textValues.Select(child => new SegmentedControlOption { Text = child })); - this.OnSelectedItemPropertyChanged(true); + items = enumerable.Cast().ToList(); } - else + + if (items != null) { - var textPropertyName = this.TextPropertyName; - if (textPropertyName != null) + var textValues = items as IEnumerable; + if (textValues == null && items.Count > 0 && items[0] is string) + { + textValues = items.Cast(); + } + + if (textValues != null) + { + segmentedControlOptions = textValues + .Select(t => new SegmentedControlOption { Text = t }) + .ToList(); + } + else { - var newChildren = new List(); + segmentedControlOptions = new List(); + var textPropertyName = this.TextPropertyName; foreach (var item in items) { - newChildren.Add(new SegmentedControlOption { Item = item, TextPropertyName = textPropertyName }); + segmentedControlOptions.Add(new SegmentedControlOption + { + Item = item, + TextPropertyName = textPropertyName + }); } - - this.Children = newChildren; - this.OnSelectedItemPropertyChanged(true); } } + else + { + segmentedControlOptions = new List(); + } } + + this.Children = segmentedControlOptions; + this.OnSelectedItemPropertyChanged(true); } protected override void OnPropertyChanged(string propertyName = null) diff --git a/Plugin.SegmentedControl.Maui/Controls/SegmentedControlOption.cs b/Plugin.SegmentedControl.Maui/Controls/SegmentedControlOption.cs index f6f9967..87b98da 100644 --- a/Plugin.SegmentedControl.Maui/Controls/SegmentedControlOption.cs +++ b/Plugin.SegmentedControl.Maui/Controls/SegmentedControlOption.cs @@ -74,10 +74,25 @@ private void OnItemPropertyChanged(object sender, PropertyChangedEventArgs e) private void SetTextFromItemProperty() { - if (this.Item != null && this.TextPropertyName != null) + if (this.Item is object item) { - var propertyInfo = this.Item.GetType().GetProperty(this.TextPropertyName); - this.Text = propertyInfo?.GetValue(this.Item)?.ToString(); + if (this.TextPropertyName is string textPropertyName && !string.IsNullOrEmpty(textPropertyName)) + { + var itemType = item.GetType(); + var propertyInfo = itemType.GetProperty(textPropertyName); + if (propertyInfo == null) + { + throw new ArgumentException($"Property '{textPropertyName}' could not be found on object of type {itemType.FullName}", nameof(this.TextPropertyName)); + } + else + { + this.Text = propertyInfo.GetValue(item)?.ToString(); + } + } + else + { + this.Text = item.ToString(); + } } } } diff --git a/Plugin.SegmentedControl.Maui/Plugin.SegmentedControl.Maui.csproj b/Plugin.SegmentedControl.Maui/Plugin.SegmentedControl.Maui.csproj index 82a62f5..1273fb2 100644 --- a/Plugin.SegmentedControl.Maui/Plugin.SegmentedControl.Maui.csproj +++ b/Plugin.SegmentedControl.Maui/Plugin.SegmentedControl.Maui.csproj @@ -36,7 +36,12 @@ https://github.com/thomasgalliker/Plugin.SegmentedControl.Maui superdev GmbH false - 1.1 + 1.2 +- Children property is no longer public; use ItemsSource instead +- Improved error handling in SegmentedControlOption +- Several bug fixes and internal refactorings + +1.1 - Add new properties FontFamily, FontSize, FontAttributes 1.0 diff --git a/Samples/SegmentedControlDemoApp/SegmentedControlDemoApp.csproj b/Samples/SegmentedControlDemoApp/SegmentedControlDemoApp.csproj index d239e93..657da18 100644 --- a/Samples/SegmentedControlDemoApp/SegmentedControlDemoApp.csproj +++ b/Samples/SegmentedControlDemoApp/SegmentedControlDemoApp.csproj @@ -8,7 +8,7 @@ 8.0.70 true enable - enable + disable SegmentedControlDemoApp diff --git a/Samples/SegmentedControlDemoApp/Views/Test1Page.xaml b/Samples/SegmentedControlDemoApp/Views/Test1Page.xaml index c7d9216..5bc5909 100644 --- a/Samples/SegmentedControlDemoApp/Views/Test1Page.xaml +++ b/Samples/SegmentedControlDemoApp/Views/Test1Page.xaml @@ -14,7 +14,31 @@ Spacing="20" VerticalOptions="Start"> -