XAML C# 从与创建者方法不同的方法访问动态创建的 Xamarin 元素
XAML C# Accessing a dynamically created Xamarin Element from a different method than the creator method
我正在尝试访问以不同方法动态创建的 xamarin 控件。似乎无法从 C# 背后的代码设置控件的 x:Name 属性。我尝试使用元素 ID,但我似乎无法弄清楚如何记录然后将其传递给另一个方法元素。我无法使用 .FindByName(请参阅之前关于 x:Name 的注释)
场景:
通过单击按钮的用户操作添加元素。然后系统通过其他几个 UI 元素询问有关控件输入的几个问题。用户填写该信息,然后单击一个按钮,该按钮的单击事件触发一个方法,将数据存储在动态创建的输入字段和选择器中。我只是不知道如何访问控件的值,因为它们采用不同的方法。
试过:
命名元素
使用 elementID,但无法从创建时传递它
创建一个循环来列出元素类型,但元素类型不是我想要的。
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)
{
}
}
}
最后一个方法实际上就是我尝试 return 来自在第一个方法中创建的控件的数据的地方。我希望该解决方案是跨平台的,但如有必要,我愿意接受其他解决方案。
在class级声明标签,你可以在整个class:
访问它
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")
{
}
}
}
}
我正在尝试访问以不同方法动态创建的 xamarin 控件。似乎无法从 C# 背后的代码设置控件的 x:Name 属性。我尝试使用元素 ID,但我似乎无法弄清楚如何记录然后将其传递给另一个方法元素。我无法使用 .FindByName(请参阅之前关于 x:Name 的注释)
场景: 通过单击按钮的用户操作添加元素。然后系统通过其他几个 UI 元素询问有关控件输入的几个问题。用户填写该信息,然后单击一个按钮,该按钮的单击事件触发一个方法,将数据存储在动态创建的输入字段和选择器中。我只是不知道如何访问控件的值,因为它们采用不同的方法。
试过: 命名元素 使用 elementID,但无法从创建时传递它 创建一个循环来列出元素类型,但元素类型不是我想要的。
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)
{
}
}
}
最后一个方法实际上就是我尝试 return 来自在第一个方法中创建的控件的数据的地方。我希望该解决方案是跨平台的,但如有必要,我愿意接受其他解决方案。
在class级声明标签,你可以在整个class:
访问它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")
{
}
}
}
}