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

时间:2018-09-18 06:54:38

标签: xamarin xamarin.forms custom-renderer

我有一个页面,其中我在具有页脚的listview中显示项目列表。列表视图的页脚仅在某些情况下可见。 这是带有条目的listview页脚。

<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实现此目的。

谢谢。

2 个答案:

答案 0 :(得分:0)

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

为您的条目命名:

 <local:AutoFocusEntry x:Name="yourEntry"

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

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

};

答案 1 :(得分:0)

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

自定义条目:

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

Android中的CustomRenderer:

[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中的CustomRenderer:

[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),我将重点放在条目上