Focused或OnFocuseChange事件不触发

本文关键字:事件 OnFocuseChange Focused | 更新日期: 2023-09-27 18:00:01

我有自定义条目,我想知道它什么时候聚焦,但从来没有发生过。我试图在自定义android类和xaml事件Focused中捕捉它,但它什么也没做。我以前也遇到过这个问题,但没有解决。这是一节课。评论块中的事件。

using Android.Support.Design.Widget;
using Android.Text;
using Android.Views;
using Android.Views.InputMethods;
using GitRemote.CustomClasses;
using GitRemote.Droid.DependencyServices;
using GitRemote.Droid.Renderers;
using GitRemote.ViewModels;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Color = Xamarin.Forms.Color;
using TextChangedEventArgs = Android.Text.TextChangedEventArgs;
using View = Android.Views.View;
[assembly: ExportRenderer(typeof(MaterialEntry), typeof(MaterialEntryRendererDroid))]
namespace GitRemote.Droid.Renderers
{
public class MaterialEntryRendererDroid : Xamarin.Forms.Platform.Android.AppCompat.ViewRenderer<Entry, View>
{
private TextInputLayout _nativeView;
    private TextInputLayout NativeView => _nativeView ?? ( _nativeView = InitializeNativeView() );
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if ( e.OldElement != null ) return;
        // MaterialEntry Render Staff
        #region
        var ctrl = CreateNativeControl();
        SetNativeControl(ctrl);
        SetText();
        SetHintText();
        SetBackgroundColor();
        SetTextColor();
        SetIsPassword();
        #endregion
        if ( Control != null )
            switch ( e.NewElement.ClassId )
            {
                case "LoginEntry":
                    ViewSaver.SaveLoginView(Control);
                    //Element.Focused += (sender, args) =>
                    //{
                    //    ViewSaver.LastView = "LoginEntry";
                    //};
                    break;
                case "PasswordEntry":
                    SetSendButtonAction();
                    ViewSaver.SavePasswordView(Control);
                    //Element.Focused += (sender, args) =>
                    //{
                    //    ViewSaver.LastView = "PasswordEntry";
                    //};
                    break;
            }
    }
    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
        if ( e.PropertyName == Entry.PlaceholderProperty.PropertyName )
        {
            SetHintText();
        }
        if ( e.PropertyName == Entry.TextColorProperty.PropertyName )
        {
            SetTextColor();
        }
        if ( e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName )
        {
            SetBackgroundColor();
        }
        if ( e.PropertyName == Entry.IsPasswordProperty.PropertyName )
        {
            SetIsPassword();
        }
        if ( e.PropertyName == Entry.TextProperty.PropertyName )
        {
            SetText();
        }
    }
    private void EditTextOnTextChanged(object sender, TextChangedEventArgs textChangedEventArgs)
    {
        Element.Text = textChangedEventArgs.Text.ToString();
        NativeView.EditText.SetSelection(Element.Text.Length);
    }
    private void SetText()
    {
        NativeView.EditText.Text = Element.Text;
    }
    private void SetIsPassword()
    {
        NativeView.EditText.InputType = Element.IsPassword
            ? InputTypes.TextVariationPassword | InputTypes.ClassText
            : InputTypes.TextVariationVisiblePassword;
    }
    public void SetBackgroundColor()
    {
        NativeView.SetBackgroundColor(Element.BackgroundColor.ToAndroid());
    }
    private void SetHintText()
    {
        NativeView.Hint = Element.Placeholder;
    }
    private void SetTextColor()
    {
        if ( Element.TextColor == Color.Default )
            NativeView.EditText.SetTextColor(NativeView.EditText.TextColors);
        else
            NativeView.EditText.SetTextColor(Element.TextColor.ToAndroid());
    }
    private TextInputLayout InitializeNativeView()
    {
        var view = FindViewById<TextInputLayout>(Resource.Id.textInputLayout);
        view.EditText.TextChanged += EditTextOnTextChanged;
        return view;
    }
    protected override View CreateNativeControl()
    {
        return LayoutInflater.From(Context).Inflate(Resource.Layout.TextInputLayout, null);
    }
    /// <summary>
    /// If Action of our entry is Send than call method from Portable
    /// </summary>
    private void SetSendButtonAction()
    {
        NativeView.EditText.EditorAction += (sender, e) =>
        {
            if ( e.ActionId == ImeAction.Send )
            {
                ( ( LoginingPageViewModel )Element.BindingContext ).OnLogInTapped();
            }
            else
                e.Handled = false;
        };
    }

}
}

Focused或OnFocuseChange事件不触发

我使用过:

 private void EditTextOnFocusChanged(object sender, FocusChangeEventArgs focusChangedEventArgs)
    {
        if ( focusChangedEventArgs.HasFocus )
            ViewSaver.SetLastView(Element.ClassId);
    }

view.EditText.FocusChange += EditTextOnFocusChanged;

它是有效的。