Xamarin.form.android Entrenderer.OnFocusChanged从未打电话



我创建了一个wentryrenderer,该wentrenderer源自entrendrenderer。我的问题很简单,我已经覆盖了入门文献中的fonocuschanged方法,因为如果出现问题,我想停止焦点传播。问题是从未调用此方法。我不明白为什么,任何人都有答案吗?

    /// <summary>
    /// Renderer Android pour le contrôl WEntry
    /// </summary>
    public class WEntryRenderer : EntryRenderer
    {
        protected override void OnFocusChanged(bool gainFocus, [GeneratedEnum] FocusSearchDirection direction, Rect previouslyFocusedRect)
        {
            bool dontSetFocus = false;
            //if (Something goes wrong)
            //{
            //    dontSetFocus = true;
            //}
            if (!dontSetFocus)
            {
                base.OnFocusChanged(gainFocus, direction, previouslyFocusedRect);
            }
        }
}

这是一个替代解决方案:

//Branching the event
        private void SubscribeEvents()
        {
            //Emit au changement de focus
            this.Control.FocusChange += WEntryRenderer_FocusChanged;
        }

//Code related
        private void WEntryRenderer_FocusChanged(object sender, FocusChangeEventArgs e)
        {
            //Si on perd le focus, on emet l'événement PropertyValidated de la propriété lié au composant
            if (!e.HasFocus && wentryRequiringFocus == null)
            {
                //Emet l'événementValidated
                this.currentWEntry.ModelPropertyBinding.OnPropertyValidated();
                //Si le composant possède des erreur et qu'aucune requête de focus n'est en cours, le composant requiert le focus
                if (!ListManager.IsNullOrEmpty(this.currentWEntry.ErrorList))
                {
                    //Place le focus sur le control courant
                    this.currentWEntry.Focus();
                    //On indique à la classe que le focus est demandé par cette instance
                    WEntryRenderer.wentryRequiringFocus = this.currentWEntry;
                }
            }
            //Si le focus a été demandé  par l'instance courante, on libère la demande à la récupération du focus
            else if (e.HasFocus && WEntryRenderer.wentryRequiringFocus == this.currentWEntry)
            {
                //Libère la requête de focus
                WEntryRenderer.wentryRequiringFocus = null;
            }
        }

i不喜欢此解决方案,即使您强制对实际实例的重点,焦点已经设置为另一个视图...它在listView

中引起了很多问题

您是否使用正确的汇编属性进行了装饰?

[assembly: ExportRenderer (typeof (Entry), typeof (WEntryRenderer))]
namespace YourProject.iOS
{
...
}

这是需要指示该渲染器的Xamarin表格,需要指定该类型(Entry)指定为第一个参数。

更新:

可以尝试的替代解决方案是在OnElementPropertyChanged中检查IsFocused属性:

protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    base.OnElementPropertyChanged(sender, e);
    if (e.PropertyName == "IsFocused")
    {
        // Do something
    }
}

最新更新