使用自定义渲染器在 xamarin 表单中可见或启用时自动对焦输入

Auto focus entry when it is visible or enabled in xamarin forms using custom renderers

我有一个页面,在该页面上我在带有页脚的列表视图中显示项目列表。列表视图的页脚仅在特定条件下可见。 这是具有条目的列表视图页脚。

<listview:SfListView.FooterTemplate>
    <DataTemplate>
        <Grid Padding="15,5,10,10" IsVisible="{Binding NewListEntryVisible}">
            <Grid.RowDefinitions>
                <RowDefinition Height="50"/>
            </Grid.RowDefinitions>    
            <local:AutoFocusEntry ReturnType="Done" IsVisible="{Binding NewListEntryVisible}" Grid.Row="0" IsEnabled="{Binding FocusEntry}" Text="{Binding cartName}" x:Name="NewListEntry" Placeholder="Enter list name">
                <local:AutoFocusEntry.Behaviors>
                    <behaviors:EventToCommandBehavior                    
                        EventName="Completed"
                        Command="{Binding NewListCommand}" />
                </local:AutoFocusEntry.Behaviors>
            </local:AutoFocusEntry>    
        </Grid>    
    </DataTemplate>    
</listview:SfListView.FooterTemplate>

一旦页脚可见(正如我之前所说,它在特定条件下可见),我希望页脚中的条目自动获得焦点。如何使用 Entry Custom Renderer 实现此目的。

提前致谢。

您想在显示或隐藏页脚的 ViewModel 字段中收听 属性 更改事件。

为您的条目命名:

 <local:AutoFocusEntry x:Name="yourEntry"

在您的 page.xaml.cs 中收听您想要的 属性 更改事件,以便为条目调用 Focus 事件:

yourViewModel.PropertyChanged += (sender, args) => {
    if (args.PropertyName.Equals("YourPropertyIsVisible"))
       yourEntry.Focus();

};

通过调用专注于自定义渲染器的方法解决了我的问题

自定义条目:

public class AutoFocusEntry : Entry
    {
        public AutoFocusEntry()
        {
        }
    }
}

Android 中的自定义渲染器:

[assembly: ExportRenderer(typeof(AutoFocusEntry), typeof(AutoFocusEntryRenderer))]
namespace Test.Mobile.Droid.CustomRenderers
{
    public class AutoFocusEntryRenderer : EntryRenderer
    {
        public AutoFocusEntryRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            MocoAutoFocusEntry entry = (MocoAutoFocusEntry)this.Element;

            if (this.Control != null && e.PropertyName == "IsVisible")
            {
                if (entry != null && entry.IsVisible)
                {
                    Control.RequestFocus();
                    InputMethodManager inputMethodManager = this.Control.Context.GetSystemService(Android.Content.Context.InputMethodService) as InputMethodManager;
                    inputMethodManager.ShowSoftInput(this.Control, ShowFlags.Forced);
                    inputMethodManager.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly);
                }
            }
        }
    }
}

iOS 中的自定义渲染器:

[assembly: ExportRenderer(typeof(AutoFocusEntry), typeof(AutoFocusEntryRenderer))]
namespace Test.Mobile.iOS.CustomRenderers
{
    public class AutoFocusEntryRenderer : EntryRenderer
    {
        public AutoFocusEntryRenderer()
        {
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            MocoAutoFocusEntry entry = (MocoAutoFocusEntry)this.Element;

            if (this.Control != null && e.PropertyName == "IsEnabled")
            {
                if (entry != null && entry.IsVisible)
                {
                    Control.BecomeFirstResponder();
                }
            }

            if (this.Control != null && e.PropertyName == "IsVisible")
            {
                if (entry != null && entry.IsVisible)
                {
                    Control.BecomeFirstResponder();
                }
            }
        }
    }
}

根据 PropertyName (IsVisible & IsEnabled),我得到了条目的焦点