CustomPickeriOS.xaml.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Collections.Specialized;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Xamarin.Essentials;
  9. using Xamarin.Forms;
  10. using Xamarin.Forms.Xaml;
  11. namespace comal.timesheets
  12. {
  13. public delegate void CustomPickeriOSValueChanged();
  14. [XamlCompilation(XamlCompilationOptions.Compile)]
  15. public partial class CustomPickeriOS : ContentView
  16. {
  17. public event CustomPickeriOSValueChanged CustomPickeriOSValueChanged;
  18. public string Title { get; set; }
  19. public List<string> StringList { get; set; }
  20. public int SelectedIndex { get; set; }
  21. public string SelectedItem { get; set; }
  22. List<PickerOption> Options = new List<PickerOption>();
  23. double fixedHeight = 0;
  24. public CustomPickeriOS()
  25. {
  26. InitializeComponent();
  27. Title = "";
  28. SelectedItem = "";
  29. StringList = new List<string>();
  30. SelectedIndex = -1;
  31. var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
  32. fixedHeight = (mainDisplayInfo.Height / 4) * 3;
  33. }
  34. public void SetDefault(string s)
  35. {
  36. selectBtn.Text = s;
  37. }
  38. public void SetBackGroundColor(Color color)
  39. {
  40. selectBtn.BackgroundColor = color;
  41. }
  42. public void AddItems(List<string> items, string defaultText = "")
  43. {
  44. foreach (string s in items)
  45. {
  46. StringList.Add(s);
  47. PickerOption option = new PickerOption { OptionString = s };
  48. Options.Add(option);
  49. }
  50. listView.ItemsSource = Options;
  51. if (!string.IsNullOrWhiteSpace(defaultText))
  52. {
  53. selectBtn.Text = defaultText;
  54. SelectedItem = defaultText;
  55. }
  56. }
  57. void SelectBtn_Clicked(object sender, EventArgs e)
  58. {
  59. Device.BeginInvokeOnMainThread(() =>
  60. {
  61. double dynamicHeight = 30 + StringList.Count * 20;
  62. if (dynamicHeight > fixedHeight)
  63. ((this.Parent) as View).HeightRequest = fixedHeight;
  64. else
  65. ((this.Parent) as View).HeightRequest = dynamicHeight;
  66. listViewRow.Height = new GridLength(1, GridUnitType.Auto);
  67. buttonRow.Height = new GridLength(0, GridUnitType.Absolute);
  68. listView.IsVisible = true;
  69. selectBtn.IsVisible = false;
  70. });
  71. }
  72. void Cell_Tapped(object sender, EventArgs e)
  73. {
  74. PickerOption option = listView.SelectedItem as PickerOption;
  75. SelectedIndex = StringList.IndexOf(option.OptionString);
  76. selectBtn.Text = option.OptionString;
  77. listView.IsVisible = false;
  78. listViewRow.Height = new GridLength(0, GridUnitType.Absolute);
  79. selectBtn.IsVisible = true;
  80. SelectedItem = option.OptionString;
  81. buttonRow.Height = new GridLength(50, GridUnitType.Absolute);
  82. ((this.Parent) as View).HeightRequest = 50;
  83. CustomPickeriOSValueChanged?.Invoke();
  84. }
  85. }
  86. public class PickerOption
  87. {
  88. public string OptionString { get; set; }
  89. public PickerOption()
  90. {
  91. OptionString = "";
  92. }
  93. }
  94. }