首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xamarin.Form.Android EntryRenderer.OnFocusChanged从未打过电话

Xamarin.Form.Android EntryRenderer.OnFocusChanged从未打过电话
EN

Stack Overflow用户
提问于 2017-09-04 11:14:21
回答 1查看 427关注 0票数 0

以下是我的问题:

我创建了一个从WEntryRenderer派生的EntryRenderer。我的问题很简单,我从EntryRenderer中高估了EntryRenderer方法,因为如果出了问题,我想停止焦点传播。问题是这个方法从来没有被调用..。我不明白为什么,有谁给我带了牙轮吗?

代码语言:javascript
复制
/// <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);
        }
    }

}

这里有一个替代解决方案:

//分支事件

代码语言:javascript
复制
    private void SubscribeEvents()
    {
        //Emit au changement de focus
        this.Control.FocusChange += WEntryRenderer_FocusChanged;
    }

//代码相关

代码语言:javascript
复制
    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;
        }
    }

我不喜欢这个解决方案,因为即使您强制将焦点放在实际实例上,焦点已经设置为另一个视图.它在ListView内部引发了许多问题

EN

回答 1

Stack Overflow用户

发布于 2017-09-04 12:09:11

你用正确的程序集属性来装饰它吗?

代码语言:javascript
复制
[assembly: ExportRenderer (typeof (Entry), typeof (WEntryRenderer))]
namespace YourProject.iOS
{

...

}

需要这样做才能指示Xamarin表单,对于指定为第一个参数的类型(Entry),需要调用此呈现程序。

更新:

可以尝试的另一种解决方案是签入OnElementPropertyChanged中的IsFocused属性:

代码语言:javascript
复制
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    base.OnElementPropertyChanged(sender, e);

    if (e.PropertyName == "IsFocused")
    {
        // Do something
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46035836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档