为什么OnFocusChanged事件被连续触发

时间:2018-11-04 16:40:58

标签: c# android xamarin.android

在Forms.Android中,当Xamarin.Forms ListView内的ViewCell中包含numericbox时,如果控件位于焦点上,则焦点更改事件将连续引发,并且sofinput(keyboard)也将连续引发。这里的numericalbox是我的自定义控件。当焦点对准控件时,将显示键盘。自定义控件由EditText继承。这是我的代码

XAML


 <StackLayout Orientation="Vertical">
   <ListView x:Name="ParentListView" ItemsSource="{Binding DataSource}" 
    Margin="20">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout>
                    <NumericTextBox Value="{Binding ProgressValue}" />

                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
   </ListView>
   </StackLayout>

C#

        protected override void OnFocusChanged(bool gainFocus, 
          FocusSearchDirection 
          direction, Rect previouslyFocusedRect)
         {
       base.OnFocusChanged(gainFocus, direction, previouslyFocusedRect);
            InputMethodManager inputmgr = 
            (InputMethodManager)this.Context.GetSystemService 
            (Context.InputMethodService);
            if (gainFocus)
            {
            if (inputmgr != null && !this.KeypadHandle)
            {
            inputmgr.ShowSoftInput(this, ShowFlags.Forced);
            }
          }
        }

0 个答案:

没有答案