3
votes

I'm brand new to xamarin and have been trying to develop a workaround to this issue: I have a Stack Layout that contains a N amount of entries, depending in what the user needs.

    <ScrollView>
        <StackLayout x:Name="Visual" Padding="5,5,5,20">
            <StackLayout x:Name="EntradasPeso" />
        </StackLayout>
    </ScrollView>

I add them dynamically, through a loop:

       //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);

        }

The problem is that, they don't have a x:Name (as far as I Know). So, how do I set this custom entry to focus the next element in the view? Having in mind that I can't do this:

         EntradasPeso.Focus(nameofentry)

Can anyone help me?

         private void Peso_Completed(object sender, EventArgs e)
         {
              // focus to next entry
         }
1
Keep a list of all the CustomEntry that you create and then you can refer back to them, assumable in the order that you want to move focus to.SushiHangover
Should i add an list to the Stack layout instead of individual entries?Leonardo Osvald de Souza
That would be one way of keeping a list of the custom entries.ClintL

1 Answers

4
votes

To focus the next entry in the layout for the Completed event this might do. For the last element the first is the next:

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();
}