XAML C#通过与创建者方法不同的方法访问动态创建的Xamarin元素

问题描述

我正在尝试访问以不同方法动态创建的xamarin控件。似乎不可能从C#背后的代码中设置控件的x:Name属性。我尝试使用元素ID,但似乎无法弄清楚如何记录然后将其传递给另一个方法元素。我无法使用.FindByName(请参阅有关x:Name的以前的注释)

场景: 通过单击按钮,通过用户操作添加元素。然后,系统通过其他一些UI元素询问有关控件输入的一些问题。用户填写该信息,然后单击按钮,该按钮的单击事件会触发一种将数据存储在动态创建的输入字段和选择器中的方法。我只是不知道如何访问控件的值,因为它们的方法不同。

已尝试: 命名元素 使用elementID,但无法从创建中传递它 创建一个循环以列出elementType,但是不希望出现元素类型。

    public int ControlCount;
    public int ControlOrder;
    public int ControlIndex;
    public string newElementType;
    public Dictionary<int,List<string>> ControlsData = new Dictionary<int,List<string>>();
    public Dictionary<int,List<string>> ControlList = new Dictionary<int,List<string>>();
    public List<string> DetailsControls = new List<string>();

    public void textField_btn_Clicked(System.Object sender,System.EventArgs e)
    {
        //indexno
        ControlIndex = ControlIndex++;

        //fieldname
        Label quest1_lbl = new Label { Margin = 5 };
        quest1_lbl.Text = "Name";
        Entry quest1_ent = new Entry { Margin = 5 };
        //Guid quest1id = quest1_bx.Id;

        //log entry?
        Label quest2_lbl = new Label { Margin = 5 };
        quest2_lbl.Text = "Log Entry";
        Picker quest2_cb = new Picker { Margin = 5 };
        var answer2_list = new List<string>();
        answer2_list.Add("Yes");
        answer2_list.Add("No");
        quest2_cb.ItemsSource = answer2_list;
        //Guid quest2id = quest2_bx.Id;

        //entry type?
        Label quest3_lbl = new Label { Margin = 5 };
        quest3_lbl.Text = "Numeric";
        Picker quest3_cb = new Picker { Margin = 5 };
        var answer3_list = new List<string>();
        quest2_cb.ItemsSource = answer2_list;
        //Guid quest3id = quest3_bx.Id;

        //add components to the question grid
        DetailsMenuCol.Children.Add(quest1_lbl);
        DetailsMenuCol.Children.Add(quest1_ent);
        DetailsMenuCol.Children.Add(quest2_lbl);
        DetailsMenuCol.Children.Add(quest2_cb);
        DetailsMenuCol.Children.Add(quest3_lbl);
        DetailsMenuCol.Children.Add(quest3_cb);
        Previewer.Width = new GridLength(0,GridUnitType.Star);
        ComponentMenu.Width = new GridLength(0,GridUnitType.Star);
        DetailsMenu.Width = new GridLength(100,GridUnitType.Star);



        Button AddComponent = new Button { BackgroundColor = Color.FromHex("#a2e0ba"),TextColor = Color.White };
        AddComponent.Text = "Create";
        AddComponent.HeightRequest = 40;
        AddComponent.Margin = 5;
        AddComponent.FontSize = 20;
        DetailsMenuCol.Children.Add(AddComponent);
        AddComponent.Clicked += new EventHandler(this.addTextField_btn_clicked);
        



    }

    void addTextField_btn_clicked(Object sender,EventArgs e)
    {


        foreach (var child in DetailsMenuCol.Children.Reverse())
        {
            var childType = child.GetType().GetElementType();
            var childTypeName = child.GetType().Name;

            if (childType is EntryCell)
            {

            }

        }

    }

最后一个方法实际上就是我试图从第一个方法中创建的控件返回数据的地方。我希望该解决方案可以跨平台,但如有必要,可以接受其他解决方案。

解决方法

在课程级别声明标签,您可以在整个课程中访问它:

public Label quest1_lbl { get; set; }
public void textField_btn_Clicked(System.Object sender,System.EventArgs e)
{

    //fieldname
    quest1_lbl = new Label { Margin = 5 };
    quest1_lbl.Text = "Name";
    Entry quest1_ent = new Entry { Margin = 5 };
    //Guid quest1id = quest1_bx.Id;

}

void addTextField_btn_clicked(Object sender,EventArgs e)
{
    quest1_lbl.Text...
}

或者给标签指定一个特定的ID,然后使用ID对其进行查询:

public void textField_btn_Clicked(System.Object sender,System.EventArgs e)
{

    //fieldname
    Label quest1_lbl = new Label { Margin = 5 };
    quest1_lbl.Text = "Name";
    quest1_lbl.ClassId = "MyLabel";

}



   void addTextField_btn_clicked(Object sender,EventArgs e)
{
    foreach (var child in DetailsMenuCol.Children.Reverse())
    {
        if (child is Label)
        {
            Label myLabel = child as Label;

            if (myLabel.ClassId == "MyLabel")
            {

            }
        }
    }
}