XAML 如何跟踪以编程方式添加的View元素?

ukdjmx9f  于 2023-03-27  发布在  其他
关注(0)|答案(1)|浏览(122)
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++;
}

通过上面的代码,我试图给予应用程序的用户能够添加和删除他们需要的entryViews
然后,我的目标是将entriesText值保存到数据库中。为此,我使用了一个名为entryValues的列表。我的问题是,当我单击某个“删除”button时,我如何从entryValues列表中删除相应的值(这是当前代码无法实现的)?

uujelgoq

uujelgoq1#

下面是在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);
    };
}

相关问题