将焦点设置为Xamarin表单中的下一个条目 [英] Set focus to Next Entry in Xamarin Forms

查看:110
本文介绍了将焦点设置为Xamarin表单中的下一个条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是xamarin的新手,并一直在尝试解决此问题的方法: 我有一个堆栈布局,其中包含N个条目,具体取决于用户的需求.

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

        }

问题是,他们没有x:Name(据我所知). 那么,如何设置此自定义条目以将视图中的下一个元素聚焦?请记住,我无法执行此操作:

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)

有人可以帮助我吗?

         private void Peso_Completed(object sender, EventArgs e)
         {
              // focus to next entry
         }

推荐答案

要在布局中为Completed事件集中下一个条目,可能会这样做.对于最后一个元素,第一个是下一个:

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

这篇关于将焦点设置为Xamarin表单中的下一个条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆