简体   繁体   中英

Set focus to Next Entry in Xamarin Forms

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 ax: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
         }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM