2014-09-12 44 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