【问题标题】:Customize Cells/ListView on Xamarin.Forms via Custom Render通过自定义渲染自定义 Xamarin.Forms 上的 Cells/ListView
【发布时间】:2015-08-19 21:23:34
【问题描述】:

我有一个本机 (Xamarin.Forms) 列表视图。我的数据上下文有 3 个属性,例如:

public class MyDataContext :
{
  public string Name {get;set;}
  public string ImageName {get;set;}
  public bool isAvailable {get;set;}
}  

其中 NametextLabel 的属性,ImageName 是 ImageControl 的图像源并且 isAvailable 某种单元格条件(true - 单元格处于活动状态,否则 Cell 具有另一个不透明度/背景并且不是 可点击,也会出现一个图像(在右上角),将显示图像“locked”!)。

我当前的列表视图:

 public class MyListView: ListView
        {
            public MyListView()
            {
                ItemTemplate = new DataTemplate(()=>
                {
                    BackgroundColor = Color.FromHex("#f2f0e9");
                        var _Label = new Label() { FontSize = 13, TextColor = Color.FromHex("#979797")};
                    _Label.SetBinding(Label.TextProperty,"Name");

                    RowHeight = 69;

                     var _Img = new Image(){ WidthRequest = 35, HeightRequest = 42};
                    _Img.SetBinding(Image.SourceProperty,"ImageName");

                        return new ViewCell 
                        {
                            View = new StackLayout
                                {
                                    Orientation = StackOrientation.Horizontal,
                                    Padding = 10,
                                    Children = 
                                        {
                                            _Img,
                                            new StackLayout
                                            {
                                                Padding = 10,
                                                VerticalOptions = LayoutOptions.Center,
                                                Spacing = 0,
                                                Children = 
                                                    {
                                                       _Label,
                                                    }
                                                }
                                        }
                                    }
                        };

                });
            }
        }
    }  

出于某种原因,我需要自定义(iOs/Android 两个平台):

  • 单元格的分隔符(需要实现一些自定义颜色和大小,如图所示)

  • 单元格禁用(类似于属性 isEnabled)与单元格的另一个不透明度/背景(以及她的控件标签/图像等):

我怎样才能做到这一点?
任何帮助将不胜感激,谢谢!
PS对不起我的英语。技能!

【问题讨论】:

    标签: c# xamarin.ios xamarin.android xamarin.forms


    【解决方案1】:

    不幸的是,我认为您将不得不为此编写自定义渲染器。为了将分隔线设置为屏幕的整个宽度,您需要将分隔符 inset 设置为零,如下所示。

    [assembly: ExportRenderer(typeof(ListView), typeof(MyListViewRenderer))]
    namespace myApp.iOS.Custom_Renderers
    {
        public class MyListViewRenderer : ListViewRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
            {
                base.OnElementChanged(e);
                var table = (UITableView)this.Control;
                table.SeperatorInset = UIEdgeInsets.Zero;
            }
        }
    }
    

    抱歉,如果上述任何语法不正确,我是凭记忆写的,但它应该会让你走上正确的道路。

    【讨论】:

    • 这显然是针对iOS版本的。
    • 很遗憾这招没有帮到我。这个 sn-p 只是隐藏了分隔符。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 2016-05-31
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多