DropDownMenuSource.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using Xamarin.Forms;
  3. using Xamarin.Forms.Platform.iOS;
  4. using System.Collections.Generic;
  5. using UIKit;
  6. using System.Linq;
  7. using Foundation;
  8. using System.Windows.Input;
  9. namespace InABox.Mobile
  10. {
  11. public class DropDownMenuSource : UITableViewSource
  12. {
  13. // Global variable for the secondary toolbar items and text to display in table row
  14. List<ToolbarItem> _tableItems;
  15. string[] _tableItemTexts;
  16. ICommand[] _tableItemActions;
  17. string CellIdentifier = "TableCell";
  18. public DropDownMenuSource(List<ToolbarItem> items)
  19. {
  20. //Set the secondary toolbar items to global variables and get all text values from the toolbar items
  21. _tableItems = items;
  22. _tableItemTexts = items.Select(a => a.Text).ToArray();
  23. _tableItemActions = items.Select(a => a.Command).ToArray();
  24. }
  25. public override nint RowsInSection(UITableView tableview, nint section)
  26. {
  27. return _tableItemTexts.Length;
  28. }
  29. public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
  30. {
  31. UITableViewCell cell = tableView.DequeueReusableCell(CellIdentifier);
  32. string item = _tableItemTexts[indexPath.Row];
  33. if (cell == null)
  34. {
  35. cell = new UITableViewCell(UITableViewCellStyle.Default, CellIdentifier);
  36. }
  37. cell.TextLabel.Text = item;
  38. return cell;
  39. }
  40. public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
  41. {
  42. return 56; // Set default row height.
  43. }
  44. public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
  45. {
  46. //Used command to excute and deselct the row and removed the table.
  47. var command = _tableItems[indexPath.Row].Command;
  48. command.Execute(_tableItems[indexPath.Row].CommandParameter);
  49. tableView.DeselectRow(indexPath, true);
  50. tableView.RemoveFromSuperview();
  51. }
  52. }
  53. }