XAML 在Xamarin中更改对已完成条目的关注,表单

abithluo  于 2022-12-16  发布在  其他
关注(0)|答案(1)|浏览(129)

我想将焦点从一个条目切换到另一个条目。
一开始,我用Focus()方法修改了所有条目,但我认为这不是好代码,因为条目越多,编辑起来就越困难。
我在这里找到了引用https://adenearnshaw.com/focus-next-element-on-enter/
一是创造行为

public class SetFocusOnEntryCompletedBehavior : Behavior
{
    public static readonly BindableProperty TargetElementProperty
       = BindableProperty.Create(nameof(TargetElement), typeof(VisualElement), typeof(SetFocusOnEntryCompletedBehavior));

    public VisualElement TargetElement
    {
        get => (VisualElement)GetValue(TargetElementProperty);
        set => SetValue(TargetElementProperty, value);
    }
        
    protected override void OnAttachedTo(BindableObject bindable)
    {
        base.OnAttachedTo(bindable);
        
        if (bindable is Entry entry)
            entry.Completed += Entry_Completed;
    }

    protected override void OnDetachingFrom(BindableObject bindable)
    {
        if (bindable is Entry entry)
            entry.Completed -= Entry_Completed;

        base.OnDetachingFrom(bindable);
    }

    private void Entry_Completed(object sender, EventArgs e)
    {
        TargetElement?.Focus();
    }
}

接下来,在xaml中实现

<ContentPage x:Class="NextFormFieldSample.Forms.MainPage"
             xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:behaviors="clr-namespace:NextFormFieldSample.Forms.Behaviors">
    <StackLayout Margin="20">   
        
        <Entry Placeholder="Field 1">
            <Entry.Behaviors>
                <behaviors:SetFocusOnEntryCompletedBehavior TargetElement="{x:Reference Entry2}" />
            </Entry.Behaviors>
        </Entry>

        <Entry x:Name="Entry2" Placeholder="Field 2"/>  
        
    </StackLayout>
</ContentPage>

但是!!!!我必须为使用这个再加一件事。

我必须在输入完成事件后接收结果;如果结果为真,则焦点改变,否则焦点再次停留。
我试着做出一些行为,但这对我来说很难。
有人能帮帮我吗?

vyu0f0g1

vyu0f0g11#

也就是说,对于数据库中的某个结果,如果结果为真,则改变焦点,如果结果为假,则焦点停留在同一个条目上,不仅改变条目的焦点,而且观察数据库中的结果。
然后,您可以为事件Entry_Completed添加一些逻辑判断。
请参考以下代码:

<ContentPage.Content> 
    <StackLayout Margin="20">
  
        <Entry x:Name="Entry1"
           Placeholder="Field 1"
           Completed="Entry_Completed"
           TabIndex="1">

        </Entry>
   
        <Entry x:Name="Entry2"
           Placeholder="Field 2"
           Completed="Entry_Completed"
           TabIndex="2">

        </Entry>        

        <Entry x:Name="Entry3"
           Placeholder="Field 3"
           Completed="Entry_Completed"
           TabIndex="3">
        </Entry>

        <Entry x:Name="Entry4"
           Placeholder="Field 4"
           Completed="Entry_Completed"
           TabIndex="4" />
    </StackLayout>
</ContentPage.Content>

您的页面中的事件Entry_Completed. xaml.cs

private void Entry_Completed(object sender, EventArgs e) 
    {
        var entry = sender as Entry; // .. and check for null

        // get result from database here

        if (entry.Text.Length == 2) // here I used string's length(2) as the logical condition
        {
            var list = (entry.Parent as StackLayout).Children; //assumes a StackLayout
            var index = list.IndexOf(entry); // 
            var nextIndex = (index + 1) >= list.Count ? 0 : index + 1; //first or next element?
            var next = list.ElementAt(nextIndex);
            next?.Focus();
        }
      }

相关问题