using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Linq; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace InABox.Mobile { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class MobileButtonStrip { /// /// The color of the Button Frames /// private readonly BindableProperty BorderColorProperty = BindableProperty.Create( nameof(BorderColor), typeof(Color), typeof(MobileButtonStrip), Color.Gray); [TypeConverter(typeof(ColorTypeConverter))] public Color BorderColor { get => (Color)GetValue(BorderColorProperty); set => SetValue(BorderColorProperty, value); } private readonly BindableProperty UnselectedBackgroundColorProperty = BindableProperty.Create( nameof(UnselectedBackgroundColor), typeof(Color), typeof(MobileButtonStrip), XF.Material.Forms.Material.Color.SecondaryVariant); [TypeConverter(typeof(ColorTypeConverter))] public Color UnselectedBackgroundColor { get => (Color)GetValue(UnselectedBackgroundColorProperty); set => SetValue(UnselectedBackgroundColorProperty, value); } private readonly BindableProperty UnselectedForegroundColorProperty = BindableProperty.Create( nameof(UnselectedForegroundColor), typeof(Color), typeof(MobileButtonStrip), XF.Material.Forms.Material.Color.OnSecondary); [TypeConverter(typeof(ColorTypeConverter))] public Color UnselectedForegroundColor { get => (Color)GetValue(UnselectedForegroundColorProperty); set => SetValue(UnselectedForegroundColorProperty, value); } private readonly BindableProperty SelectedBackgroundColorProperty = BindableProperty.Create( nameof(SelectedBackgroundColor), typeof(Color), typeof(MobileButtonStrip), XF.Material.Forms.Material.Color.Surface); [TypeConverter(typeof(ColorTypeConverter))] public Color SelectedBackgroundColor { get => (Color)GetValue(SelectedBackgroundColorProperty); set => SetValue(SelectedBackgroundColorProperty, value); } private readonly BindableProperty SelectedForegroundColorProperty = BindableProperty.Create( nameof(SelectedForegroundColor), typeof(Color), typeof(MobileButtonStrip), XF.Material.Forms.Material.Color.OnSurface); [TypeConverter(typeof(ColorTypeConverter))] public Color SelectedForegroundColor { get => (Color)GetValue(SelectedForegroundColorProperty); set => SetValue(SelectedForegroundColorProperty, value); } private readonly BindableProperty CornerRadiusProperty = BindableProperty.Create( nameof(CornerRadius), typeof(float), typeof(MobileButtonStrip), 5f); public float CornerRadius { get => (float)GetValue(CornerRadiusProperty); set => SetValue(CornerRadiusProperty, value); } public double ItemSpacing { get => _list.Spacing; set => _list.Spacing = value; } private readonly BindableProperty FontSizeProperty = BindableProperty.Create( nameof(FontSize), typeof(double), typeof(MobileButtonStrip), Device.GetNamedSize(NamedSize.Small, typeof(Label))); [TypeConverter(typeof(FontSizeConverter))] public double FontSize { get => (double)GetValue(FontSizeProperty); set => SetValue(FontSizeProperty, value); } private readonly BindableProperty FontAttributesProperty = BindableProperty.Create( nameof(FontAttributes), typeof(FontAttributes), typeof(MobileButtonStrip), FontAttributes.None); public FontAttributes FontAttributes { get => (FontAttributes)GetValue(FontAttributesProperty); set => SetValue(FontAttributesProperty, value); } public MobileButtonStripItem SelectedItem { get => Items.FirstOrDefault(x => x.Selected); set { foreach (var item in Items) item.Selected = item == value; } } public event EventHandler SelectionChanged; public MobileButtonStripItems Items { get; private set; } public MobileButtonStrip() { HeightRequest = 30; var items = new MobileButtonStripItems(); items.CollectionChanged += ItemsChanged; Items = items; InitializeComponent(); _list.BindingContext = Items; _list.SetValue(BindableLayout.ItemsSourceProperty, Items); //_list.ItemsSource = Items; } private void ItemsChanged(object sender, NotifyCollectionChangedEventArgs e) { Device.BeginInvokeOnMainThread(() => { SelectedItem ??= Items.FirstOrDefault(); }); } private void DoTap(object sender, EventArgs e) { Device.BeginInvokeOnMainThread(() => { foreach (var item in Items) item.Selected = item == (sender as Label)?.BindingContext; OnPropertyChanged(nameof(Items)); SelectionChanged?.Invoke(this,EventArgs.Empty); }); } } }