123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using InABox.Mobile;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- namespace PRS.Mobile
- {
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class StringList : ContentView
- {
- private List<DeleteableEditor> _editors = new List<DeleteableEditor>();
- private ImageButton _add;
- public StringList()
- {
- InitializeComponent();
- _add = _addbutton;
- }
- private void AddBtn_Clicked(object sender, EventArgs e)
- {
- _editors.Add(CreateEditor());
- LoadLayout();
- }
- private void LoadLayout()
- {
- _grid.Children.Clear();
- _grid.RowDefinitions.Clear();
- foreach (var editor in _editors)
- {
- _grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto } );
- editor.SetValue(Grid.RowProperty,_grid.RowDefinitions.Count-1);
- _grid.Children.Add(editor);
- }
- if (!_editors.Any())
- _grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto } );
- _add.SetValue(Grid.RowProperty, _grid.RowDefinitions.Count-1);
- _add.SetValue(Grid.ColumnProperty, 1);
- _grid.Children.Add(_add);
- }
- private DeleteableEditor CreateEditor(string text = "")
- {
- DeleteableEditor editor = new DeleteableEditor(text);
- editor.OnEditorDeleted += () =>
- {
- _editors.Remove(editor);
- if (!_editors.Any())
- _editors.Add(CreateEditor());
- LoadLayout();
- };
- return editor;
- }
- public void LoadList(string[] items)
- {
- _editors.Clear();
- foreach (var item in items)
- _editors.Add(CreateEditor(item));
- LoadLayout();
- }
- public string[] SaveItems()
- {
- List<string> items = new List<string>();
- foreach (DeleteableEditor entry in _editors)
- {
- if (!string.IsNullOrEmpty(entry.Text))
- items.Add(entry.Text);
- }
- return items.ToArray();
- }
- }
- public delegate void EditorDeleted();
- public class DeleteableEditor : Grid
- {
- public event EditorDeleted OnEditorDeleted;
- public string Text { get; set; }
- public DeleteableEditor(string text = "")
- {
- Text = text;
- Setup();
- }
- private void Setup()
- {
- ColumnSpacing = 0;
- ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
- ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
- ImageButton img = new ImageButton
- {
- Source = "cross",
- HeightRequest = 40,
- WidthRequest = 40
- };
- img.Clicked += (o,e) => OnEditorDeleted?.Invoke();
- Grid.SetColumn(img, 0);
- var edt = new Entry
- {
- Text = Text,
- FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Entry)),
- Keyboard = Keyboard.Plain,
- BackgroundColor = Color.LightYellow,
- TextColor = Color.Black
- };
- edt.TextChanged += (sender, e) =>
- {
- Text = edt.Text;
- };
- MobileCard card = new MobileCard()
- {
- Content = edt
- };
- Grid.SetColumn(card,1);
-
- Children.Add(card);
- Children.Add(img);
- }
-
- }
- }
|