CustomPickeriOS.xaml.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 PRS.Mobile
  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 JobDocFilterType Type { get; set; }
  25. public CustomPickeriOS()
  26. {
  27. InitializeComponent();
  28. Title = "";
  29. SelectedItem = "";
  30. StringList = new List<string>();
  31. SelectedIndex = -1;
  32. var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
  33. fixedHeight = (mainDisplayInfo.Height / 4) * 3;
  34. }
  35. public void SetDefault(string s)
  36. {
  37. selectBtn.Text = s;
  38. }
  39. public void SetBackGroundColor(Color color)
  40. {
  41. selectBtn.BackgroundColor = color;
  42. }
  43. public void AddItems(List<string> items, string defaultText = "")
  44. {
  45. foreach (string s in items)
  46. {
  47. StringList.Add(s);
  48. PickerOption option = new PickerOption { OptionString = s };
  49. Options.Add(option);
  50. }
  51. listView.ItemsSource = Options;
  52. if (!string.IsNullOrWhiteSpace(defaultText))
  53. {
  54. selectBtn.Text = defaultText;
  55. SelectedItem = defaultText;
  56. }
  57. }
  58. void SelectBtn_Clicked(object sender, EventArgs e)
  59. {
  60. Device.BeginInvokeOnMainThread(() =>
  61. {
  62. double dynamicHeight = 30 + StringList.Count * 20;
  63. if (dynamicHeight > fixedHeight)
  64. ((this.Parent) as View).HeightRequest = fixedHeight;
  65. else
  66. ((this.Parent) as View).HeightRequest = dynamicHeight;
  67. listViewRow.Height = new GridLength(1, GridUnitType.Auto);
  68. buttonRow.Height = new GridLength(0, GridUnitType.Absolute);
  69. listView.IsVisible = true;
  70. selectBtn.IsVisible = false;
  71. });
  72. }
  73. void Cell_Tapped(object sender, EventArgs e)
  74. {
  75. PickerOption option = listView.SelectedItem as PickerOption;
  76. SelectedIndex = StringList.IndexOf(option.OptionString);
  77. selectBtn.Text = option.OptionString;
  78. listView.IsVisible = false;
  79. listViewRow.Height = new GridLength(0, GridUnitType.Absolute);
  80. selectBtn.IsVisible = true;
  81. SelectedItem = option.OptionString;
  82. buttonRow.Height = new GridLength(50, GridUnitType.Absolute);
  83. ((this.Parent) as View).HeightRequest = 50;
  84. CustomPickeriOSValueChanged?.Invoke();
  85. }
  86. }
  87. public class PickerOption
  88. {
  89. public string OptionString { get; set; }
  90. public PickerOption()
  91. {
  92. OptionString = "";
  93. }
  94. }
  95. }