如何动态添加选择器

How to dynamically add a picker

I have a button on my screen and when I press the button I want a picker to show up with a list of items and when an item is chosen the text of the button should change to that and the picker should go离开。

这是我的代码:

Picker picker = new Picker
    {
        Title = "What's in the slot?",
        VerticalOptions = LayoutOptions.CenterAndExpand
        //HorizontalOptions = LayoutOptions.Center 

    };

按下按钮时调用的函数:

private void Displaypickerview(int row, int column)
    {
        if (status == "filling board")
        {
            foreach (string text in pickerText)
            {
                picker.Items.Add(text);
            }
            foreach (string ore in oreLevels)
            {
                picker.Items.Add(ore);
            }


            picker.SelectedIndexChanged += (sender, args) =>
            {
                if (picker.SelectedIndex == -1)
                {

                }
                else
                {
                    //change value of cell and button
                    Picker picker = (Picker)sender;
                    int index = picker.SelectedIndex;

                    if (index < pickerText.Length)
                    {
                        board[row, column].Text = pickerText[index - 1];
                    }
                    else {
                        board[row, column].Text = oreLevels[index - 1 - pickerText.Length];
                    }
                }
            };
        }
        else if (status == "choosing item")
        {

        }

    }

但我不知道我应该如何在屏幕上呈现选择器视图并随后将其删除。

更新:

下图中done按钮的事件处理程序是什么

您可以将选择器添加到您的 Grid 或您正在使用的任何 Panel,方法是将其添加到 Children 集合:

Grid.SetRow(picker, 0); //first row
Grid.SetColumn(picker, 0); //first column
grid.Children.Add(picker);

同样,您可以通过将其从 Children 集合中删除来将其删除:

grid.Children.Remove(picker);