我是xamarin的新手,并一直在尝试解决此问题的方法: 我有一个堆栈布局,其中包含N个条目,具体取决于用户的需求。
<ScrollView>
<StackLayout x:Name="Visual" Padding="5,5,5,20">
<StackLayout x:Name="EntradasPeso" />
</StackLayout>
</ScrollView>
我通过循环动态添加它们:
//calculo.Qnt_amostra is the ammount of entries that the view has.
for (int i = 1; i <= (int)calculo.Qnt_amostra; i++)
{
CustomEntry peso = new CustomEntry();
peso.IsNumeric = true;
peso.Placeholder = $"Peso da {i}° amostra";
peso.Keyboard = Keyboard.Numeric;
peso.Completed += Peso_Completed;
EntradasPeso.Children.Add(peso);
}
问题是,他们没有x:Name(据我所知)。 因此,如何设置此自定义条目以将视图中的下一个元素聚焦?请记住,我不能这样做:
EntradasPeso.Focus(nameofentry)
有人可以帮助我吗?
private void Peso_Completed(object sender, EventArgs e)
{
// focus to next entry
}
答案 0 :(得分:1)
这可以使Completed
事件集中在布局中的下一个条目。对于最后一个元素,第一个是下一个:
private void Peso_Completed(object sender, EventArgs e)
{
var entry = sender as MyEntry; // .. and check for null
var list = (entry.Parent as StackLayout).Children; //assumes a StackLayout
var index = list.IndexOf(entry); // what if IndexOf returns -1?
var nextIndex = (index + 1) >= list.Count ? 0 : index + 1; //first or next element?
var next = list.ElementAt(nextIndex);
next?.Focus();
}