StringList.xaml.cs 3.6 KB

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