StringList.xaml.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using InABox.Mobile;
  7. using Xamarin.Forms;
  8. using Xamarin.Forms.Xaml;
  9. namespace PRS.Mobile
  10. {
  11. [XamlCompilation(XamlCompilationOptions.Compile)]
  12. public partial class StringList : ContentView
  13. {
  14. private List<DeleteableEditor> _editors = new List<DeleteableEditor>();
  15. private ImageButton _add;
  16. public StringList()
  17. {
  18. InitializeComponent();
  19. _add = _addbutton;
  20. }
  21. private void AddBtn_Clicked(object sender, EventArgs e)
  22. {
  23. _editors.Add(CreateEditor());
  24. LoadLayout();
  25. }
  26. private void LoadLayout()
  27. {
  28. _grid.Children.Clear();
  29. _grid.RowDefinitions.Clear();
  30. foreach (var editor in _editors)
  31. {
  32. _grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto } );
  33. editor.SetValue(Grid.RowProperty,_grid.RowDefinitions.Count-1);
  34. _grid.Children.Add(editor);
  35. }
  36. if (!_editors.Any())
  37. _grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto } );
  38. _add.SetValue(Grid.RowProperty, _grid.RowDefinitions.Count-1);
  39. _add.SetValue(Grid.ColumnProperty, 1);
  40. _grid.Children.Add(_add);
  41. }
  42. private DeleteableEditor CreateEditor(string text = "")
  43. {
  44. DeleteableEditor editor = new DeleteableEditor(text);
  45. editor.OnEditorDeleted += () =>
  46. {
  47. _editors.Remove(editor);
  48. if (!_editors.Any())
  49. _editors.Add(CreateEditor());
  50. LoadLayout();
  51. };
  52. return editor;
  53. }
  54. public void LoadList(string[] items)
  55. {
  56. _editors.Clear();
  57. foreach (var item in items)
  58. _editors.Add(CreateEditor(item));
  59. LoadLayout();
  60. }
  61. public string[] SaveItems()
  62. {
  63. List<string> items = new List<string>();
  64. foreach (DeleteableEditor entry in _editors)
  65. {
  66. if (!string.IsNullOrEmpty(entry.Text))
  67. items.Add(entry.Text);
  68. }
  69. return items.ToArray();
  70. }
  71. }
  72. public delegate void EditorDeleted();
  73. public class DeleteableEditor : Grid
  74. {
  75. public event EditorDeleted OnEditorDeleted;
  76. public string Text { get; set; }
  77. public DeleteableEditor(string text = "")
  78. {
  79. Text = text;
  80. Setup();
  81. }
  82. private void Setup()
  83. {
  84. ColumnSpacing = 0;
  85. ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
  86. ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  87. ImageButton img = new ImageButton
  88. {
  89. Source = "cross",
  90. HeightRequest = 40,
  91. WidthRequest = 40
  92. };
  93. img.Clicked += (o,e) => OnEditorDeleted?.Invoke();
  94. Grid.SetColumn(img, 0);
  95. var edt = new Entry
  96. {
  97. Text = Text,
  98. FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Entry)),
  99. Keyboard = Keyboard.Plain,
  100. BackgroundColor = Color.LightYellow,
  101. TextColor = Color.Black
  102. };
  103. edt.TextChanged += (sender, e) =>
  104. {
  105. Text = edt.Text;
  106. };
  107. MobileCard card = new MobileCard()
  108. {
  109. Content = edt
  110. };
  111. Grid.SetColumn(card,1);
  112. Children.Add(card);
  113. Children.Add(img);
  114. }
  115. }
  116. }