12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- namespace comal.timesheets
- {
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class StringList : ContentView
- {
- public StringList()
- {
- InitializeComponent();
- }
- private void AddBtn_Clicked(object sender, EventArgs e)
- {
- stackLayout.Children.Add(
- CreateEditor()
- );
- }
- private DeleteableEditor CreateEditor(string text = "")
- {
- DeleteableEditor editor = new DeleteableEditor(text);
- editor.OnEditorDeleted += () => { stackLayout.Children.Remove(editor); };
- return editor;
- }
- public void LoadList(string[] items)
- {
- foreach (var item in items)
- {
- stackLayout.Children.Add(CreateEditor(item));
- }
- }
- public string[] SaveItems()
- {
- List<string> items = new List<string>();
- foreach (DeleteableEditor entry in stackLayout.Children)
- {
- 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()
- {
- ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
- ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
- var edt = new Entry();
- edt.Text = Text;
- edt.FontSize = 16;
- edt.TextChanged += (sender, e) =>
- {
- Text = edt.Text;
- };
- Grid.SetColumn(edt, 0);
- Image img = new Image { Source = "closee.png", HeightRequest = 20, WidthRequest = 20, Margin = new Thickness(2,2,10,2) };
- img.GestureRecognizers.Add(new TapGestureRecognizer { Command = new Command(Close) });
- Grid.SetColumn(img, 1);
- Children.Add(edt);
- Children.Add(img);
- }
- private void Close(object obj)
- {
- OnEditorDeleted?.Invoke();
- }
- }
- }
|