2016-10-18 105 views
1

我爲Xamarin.Forms Android應用程序定製了一個條目。目前,我正在使用自定義渲染器爲條目賦予一個帶有邊框的橢圓形狀。更改焦點條目的邊框顏色

我也想更改焦點上的條目邊框的顏色,並在未聚焦時恢復原始顏色。

我的自定義渲染低於:

public class EntryCellRenderer : EntryRenderer 
{ 
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) 
    { 
     base.OnElementChanged(e); 
     var UIEntry = (e.NewElement != null) ? (EntryCell)e.NewElement : (EntryCell)e.OldElement; 

     if (this.Control != null) 
     { 
      Control.Gravity = Android.Views.GravityFlags.CenterVertical; 
      Control.SetPadding(30, 30, 30, 31); 
      GradientDrawable gd = new GradientDrawable(); 
      gd.SetShape(ShapeType.Rectangle); 

      var BackgroundColor = ColorHelper.FromHex(CoreTheme.COLOR_DEFAULT_CLEAR); 
      var ColorRef = Android.Graphics.Color.Argb(
       (byte)(BackgroundColor.A * 255), 
       (byte)(BackgroundColor.R * 255), 
       (byte)(BackgroundColor.G * 255), 
       (byte)(BackgroundColor.B * 255)); 


      gd.SetColor((int)ColorRef); 

      UIEntry.BackgroundColor = Xamarin.Forms.Color.Transparent; 
      gd.SetStroke(7, Android.Graphics.Color.LightGray); 
      gd.SetCornerRadius(20.0f); 
      Control.SetBackground(gd); 

     } 
    } 
} 

我不知道如何與有什麼我想做的事情上面這個自定義項中的焦點事件進行。

回答

2

在你OnElementChanged你就可以將一個事件到Focused事件:

e.NewElement.Unfocused += (sender, evt) => 
{ 
    // unfocused, set color 
}; 
e.NewElement.Focused += (sender, evt) => 
{ 
    // focus, set color 
}; 

FYI:該OnNativeFocusChanged將使用一個代理做這個完美的地方,但它是不公開...

internal virtual void OnNativeFocusChanged(bool hasFocus) 
{ 
} 
+0

好吧,這很有用,但是當我嘗試將焦點從我的作品中解放出來時,它不會讓我不專注或改變顏色。是否有一個原因? – Euridice01

+0

@ Euridice01不知道我明白。當視圖焦點變爲未聚焦時,您沒有獲得「聚焦」事件調用?要麼...?即這條線沒有被調用? https://github.com/xamarin/Xamarin.Forms/blob/4f8af14f13f34d9b66d92e315c8486edd28e851a/Xamarin.Forms.Platform.Android/ViewRenderer.cs#L62 – SushiHangover

+0

因此,在頁面加載...條目不集中。然後我點擊它,它很專注。在完成輸入內容後,點擊條目之外....它不會變得不專心。此頁面從ContentPage繼承(不確定是否調用了ViewRenderer?)。我怎樣才能最好地調試呢?順便說一句,我正在使用Xamarin.Forms 1.5.6 ...不知道它是否是一個錯誤。 – Euridice01

0

當您的Focus事件被調用時,您可以連接Action以運行。所以,你會新Action屬性添加到您的自定義EntryCell也連線了Focus

public class CustomEntryCell : EntryCell { 

    public Action OnFocusEventAction { get; set; } 

    public CustomEntryCell() { Focused += OnFocused } 

    private void OnFocused(object sender, FocusEventArgs focusEventArgs) { OnFocusEventAction?.Invoke(); } 

    ///Or if you are not at C# 6 yet: 
    //private void OnFocused(object sender, FocusEventArgs focusEventArgs) { 
    // Action action = OnFocusEventAction; 
    // if(action != null) { action.Invoke(); } 
    //} 
} 

然後你自定義呈現將分配的東西在控制的Action

public class EntryCellRenderer : EntryRenderer { 
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) { 
     .... 
     UIEntry.OnFocusEventAction =() => //Your custom border color change code here 
     .... 
    } 
} 

把這一切出來的記憶和在這個文本框中,所以如果有什麼不工作,讓我知道,我會花更多的時間!

+0

似乎可以工作,但是當我在條目外點擊時,條目不會變得不專心。該條目位於內容頁面上。 – Euridice01

+0

我創建了一個單獨的unFocus事件,它的工作原理。看起來像將它添加到焦點事件不會觸發unFocus ....我想也許這是Xamarin.Forms的錯誤? – Euridice01