2017-09-04 52 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。 –

相關問題