2017-09-04 50 views
0

这里是我的问题:Xamarin.Form.Android EntryRenderer.OnFocusChanged从来没有所谓的

我创造了一个WEntryRenderer至极从EntryRenderer派生。我的问题很简单,我已经从EntryRenderer中覆盖了OnFocusChanged方法,因为如果出现错误,我想停止焦点传播。问题是,这种方法从来没有被称为..我不明白为什么,有没有人有我的想法?

/// <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); 
     } 
    } 

}

下面是一个替代的解决方案:

//分支事件

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

//代码相关

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中造成很多问题

回答

0

你是否用正确的程序集属性装饰它?

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

... 

} 

这是必要的,以指示此呈示需要调用指定为第一个参数的类型(Entry)Xamarin形态。

UPDATE:

另一种解决方案可以尝试在你的OnElementPropertyChanged来检查IsFocused属性:

protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) 
{ 
    base.OnElementPropertyChanged(sender, e); 

    if (e.PropertyName == "IsFocused") 
    { 
     // Do something 
    } 
} 
+0

嗨感谢您的awnser :)是的,我做到了。除了这种方法以外的所有东西都可以很好地工作我有一个OnElementPropertyChanged,OnElementChanged,这个wokrs ...但是这个OnFocusChange永远不会被调用=( –

+0

我更新了一个替代解决方案,你可以试试。 –

+0

我增加了一些信息:)。使用您的解决方案,焦点将设置为etherway = /我想停止FocusSet。 –

相关问题