CustomPickeriOS.xaml.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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)
  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. }
  52. void SelectBtn_Clicked(object sender, EventArgs e)
  53. {
  54. Device.BeginInvokeOnMainThread(() =>
  55. {
  56. double dynamicHeight = 30 + StringList.Count * 20;
  57. if (dynamicHeight > fixedHeight)
  58. ((this.Parent) as View).HeightRequest = fixedHeight;
  59. else
  60. ((this.Parent) as View).HeightRequest = dynamicHeight;
  61. listViewRow.Height = new GridLength(1, GridUnitType.Auto);
  62. buttonRow.Height = new GridLength(0, GridUnitType.Absolute);
  63. listView.IsVisible = true;
  64. selectBtn.IsVisible = false;
  65. });
  66. }
  67. void Cell_Tapped(object sender, EventArgs e)
  68. {
  69. PickerOption option = listView.SelectedItem as PickerOption;
  70. SelectedIndex = StringList.IndexOf(option.OptionString);
  71. selectBtn.Text = option.OptionString;
  72. listView.IsVisible = false;
  73. listViewRow.Height = new GridLength(0, GridUnitType.Absolute);
  74. selectBtn.IsVisible = true;
  75. SelectedItem = option.OptionString;
  76. buttonRow.Height = new GridLength(50, GridUnitType.Absolute);
  77. ((this.Parent) as View).HeightRequest = 50;
  78. CustomPickeriOSValueChanged?.Invoke();
  79. }
  80. }
  81. public class PickerOption
  82. {
  83. public string OptionString { get; set; }
  84. public PickerOption()
  85. {
  86. OptionString = "";
  87. }
  88. }
  89. }