【问题标题】:Xamarin.Forms CustomRenderer for a TextCellTextCell 的 Xamarin.Forms CustomRenderer
【发布时间】:2014-11-06 18:32:54
【问题描述】:

我第一次尝试创建自定义渲染器。我要做的就是更改 ListView 中 TextCell 的字体大小。

我已经在http://developer.xamarin.com/guides/cross-platform/xamarin-forms/custom-renderer/ 处查看了入口单元格的指南,但不知道如何处理 TextCell 并且无法在任何地方找到信息(它可能非常基本,但我对 Xamarin 很陌生)

Entry 单元格的代码如下(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 然后它不喜欢它 --- 我没有知道该怎么做,这让我发疯了

任何建议表示赞赏

【问题讨论】:

    标签: xamarin.android xamarin.forms custom-renderer


    【解决方案1】:

    不确定这是否是您要查找的内容,但这应该可以。您可以操作TextCellText 视图和Detail 视图。

    我认为您最好使用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));
        }
    }
    

    【讨论】:

    • 您好,感谢您的回复。我已尝试使用您的解决方案,但我有一个小问题,由于我缺乏经验,我需要一些建议
    • 从头开始 - 我让它工作了,所以谢谢。你提到它在 ViewCell 中会更好,你能给我设置正确的方向吗 - 我需要在程序集中使用什么(我在上面的示例中使用了 ExportRenderer),我需要覆盖什么方法等,或者是有一个以易于理解的方式提供此信息的好网站吗?干杯
    • 看看这篇关于如何使用ViewCell的文章。 motzcod.es/post/93792500152/…
    猜你喜欢
    • 1970-01-01
    • 2015-03-07
    • 2016-08-05
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 2017-10-07
    • 2021-06-20
    • 2018-08-05
    相关资源
    最近更新 更多