Search code examples
c#xamlxamarin.forms

How to keep track of programmatically added View elements?


int counter = 0;

private void AddLayout()
{

    List<string> entryValues = new List<string>();
    
    List<StackLayout> stackList = new List<StackLayout>();

    StackLayout horizontalLayout = new StackLayout()
                            {
                                Orientation = StackOrientation.Horizontal,
                                Children =
                                {
                                    new Entry(),

                                    new Button
                                    {
                                        Text = "Add"
                                    },

                                    new Button
                                    {
                                        Text = "Remove"
                                    }
                                }
                            };

    container.Children.Add(horizontalLayout);

    stackList.Add(horizontalLayout);

    stackList[counter].Children.OfType<Entry>().FirstOrDefault().Unfocused += (o, ev) =>
    {
        entryValues.Add(horizontalLayout.Children.OfType<Entry>().FirstOrDefault().Text);
    };


    stackList[counter].Children.OfType<Button>().FirstOrDefault().Clicked += (o, ev) =>
    {
       container.Children.Add(horizontalLayout)
    };

    stackList[counter].Children.OfType<Button>().ElementAt(1).Clicked += (o, ev) =>
    {                            
        container.Children.Remove(horizontalLayout);

        counter--;
        stackList.RemoveAt(counter);
        entryValues.RemoveAt(counter);
    };

    counter++;
}

With the code above I try to give to the app's users the ability to add and remove as many entry Views they need. So far, so good.

Then my goal is to save the Text values of the entries to a database. For that purpose I use a list called entryValues. My question is when I click a certain "Remove" button how can I remove the corresponding value from the entryValues list (which is something that the current code does not achieve)?


Solution

  • Here is the code that works with the help of a Dictionary:

    private void AddLayout()
    {
    
        Dictionary<StackLayout, string> stackLayoutDictionary = new Dictionary<StackLayout, string>();
    
        StackLayout horizontalLayout = new StackLayout()
                                {
                                    Orientation = StackOrientation.Horizontal,
                                    Children =
                                    {
                                        new Entry(),
    
                                        new Button
                                        {
                                            Text = "Add"
                                        },
    
                                        new Button
                                        {
                                            Text = "Remove"
                                        }
                                    }
                                };
    
        container.Children.Add(horizontalLayout);
    
        stackLayoutDictionary.Add(horizontalLayout, "");
    
        horizontalLayout.Children.OfType<Entry>().FirstOrDefault().Unfocused += (o, ev) =>
        {
            stackLayoutDictionary[horizontalLayout] = horizontalLayout.Children.OfType<Entry>().FirstOrDefault().Text;
        };
    
    
        horizontalLayout.Children.OfType<Button>().FirstOrDefault().Clicked += (o, ev) =>
        {
           container.Children.Add(horizontalLayout);
        };
    
        horizontalLayout.Children.OfType<Button>().ElementAt(1).Clicked += (o, ev) =>
        {                            
            container.Children.Remove(horizontalLayout);
    
            stackLayoutDictionary.Remove(horizontalLayout);
        };
    }