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.Essentials; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace comal.timesheets { public delegate void CustomPickeriOSValueChanged(); [XamlCompilation(XamlCompilationOptions.Compile)] public partial class CustomPickeriOS : ContentView { public event CustomPickeriOSValueChanged CustomPickeriOSValueChanged; public string Title { get; set; } public List StringList { get; set; } public int SelectedIndex { get; set; } public string SelectedItem { get; set; } List Options = new List(); double fixedHeight = 0; public CustomPickeriOS() { InitializeComponent(); Title = ""; SelectedItem = ""; StringList = new List(); SelectedIndex = -1; var mainDisplayInfo = DeviceDisplay.MainDisplayInfo; fixedHeight = (mainDisplayInfo.Height / 4) * 3; } public void SetDefault(string s) { selectBtn.Text = s; } public void SetBackGroundColor(Color color) { selectBtn.BackgroundColor = color; } public void AddItems(List items, string defaultText = "") { foreach (string s in items) { StringList.Add(s); PickerOption option = new PickerOption { OptionString = s }; Options.Add(option); } listView.ItemsSource = Options; if (!string.IsNullOrWhiteSpace(defaultText)) { selectBtn.Text = defaultText; SelectedItem = defaultText; } } void SelectBtn_Clicked(object sender, EventArgs e) { Device.BeginInvokeOnMainThread(() => { double dynamicHeight = 30 + StringList.Count * 20; if (dynamicHeight > fixedHeight) ((this.Parent) as View).HeightRequest = fixedHeight; else ((this.Parent) as View).HeightRequest = dynamicHeight; listViewRow.Height = new GridLength(1, GridUnitType.Auto); buttonRow.Height = new GridLength(0, GridUnitType.Absolute); listView.IsVisible = true; selectBtn.IsVisible = false; }); } void Cell_Tapped(object sender, EventArgs e) { PickerOption option = listView.SelectedItem as PickerOption; SelectedIndex = StringList.IndexOf(option.OptionString); selectBtn.Text = option.OptionString; listView.IsVisible = false; listViewRow.Height = new GridLength(0, GridUnitType.Absolute); selectBtn.IsVisible = true; SelectedItem = option.OptionString; buttonRow.Height = new GridLength(50, GridUnitType.Absolute); ((this.Parent) as View).HeightRequest = 50; CustomPickeriOSValueChanged?.Invoke(); } } public class PickerOption { public string OptionString { get; set; } public PickerOption() { OptionString = ""; } } }