2014-09-12 39 views
1

我想第一次創建自定義渲染器。我想要做的就是在ListView中更改TextCell的字體大小。Xamarin.Forms TextCell CustomRenderer

我已經在這裏看到http://developer.xamarin.com/guides/cross-platform/xamarin-forms/custom-renderer/的指南條目細胞,但不知道該怎麼對TextCell做,不能在任何地方找到的信息(這可能是很基本的,但我是很新的Xamarin)

爲入門細胞的代碼是(Android版)

public class MyEntryRenderer : EntryRenderer 
{ 
    // Override the OnElementChanged method so we can tweak this renderer post-initial setup 
    protected override void OnElementChanged (ElementChangedEventArgs<Entry> e) 
    { 
    base.OnElementChanged (e)); 
    if (e.OldElement == null) { // perform initial setup 
     // lets get a reference to the native control 
     var nativeEditText = (global::Android.Widget.EditText) Control; 
     // do whatever you want to the textField here! 
     nativeEditText.SetBackgroundColor(global::Android.Graphics.Color.DarkGray); 
    } 
    } 
} 

所以在TextCell我是什麼壓倒一切的情況下,如下所示? (如果我使用OnElementChanged,它不會給我OnElementChanged的基礎 - 它確實給了我OnCellPropertyChanged,但如果我使用該方法,那麼它似乎想要PropertyChangedEventArgs,那麼它不喜歡它---我沒有知道該怎麼做,這是推動我堅果

任何建議表示讚賞

回答

3

不知道這是你要找的是什麼,但是這應該工作。你可以操縱這兩個文字視圖以及查看詳情查看TextCell

我想你會更好用ViewCell雖然,因爲你可以更多地控制包含什麼以及它是如何呈現的。

class MyTextCellRenderer : TextCellRenderer 
{ 
    protected override View GetCellCore(Cell item, View convertView, ViewGroup parent, Context context) 
    { 
     var cell = (LinearLayout) base.GetCellCore(item, convertView, parent, context); 
     var textView = (TextView)(cell.GetChildAt(1) as LinearLayout).GetChildAt(0); 
     var detailView = (TextView)(cell.GetChildAt(1) as LinearLayout).GetChildAt(1); 
     textView.TextSize = textView.DipsToPixels(32); 
     return cell; 
    } 
} 

public static class LayoutHelperExtensions 
{ 
    public static int DipsToPixels(this View view, float dip) 
    { 
     return (int) Math.Round(TypedValue.ApplyDimension(ComplexUnitType.Dip, dip, view.Resources.DisplayMetrics)); 
    } 
} 
+0

嗨,謝謝你的回覆。我試圖使用你的解決方案,但我有一個小問題,由於我的經驗不足,我需要一些建議 – user1667474 2014-09-13 01:42:37

+0

劃痕 - 我得到它的工作如此謝謝。你提到它在ViewCell中會更好,你可以將我設置在正確的方向 - 我需要在程序集中使用什麼(我已經使用了ExportRenderer作爲上面的例子),我需要什麼方法來覆蓋等,或者是有一個很好的網站,有一個容易理解的方式這個信息?乾杯 – user1667474 2014-09-13 02:17:37

+0

看看這篇關於如何使用ViewCell的文章。 http://motzcod.es/post/93792500152/custom-listview-viewcells-in-xamarin-forms – Kiliman 2014-09-13 15:28:50