自定义ListView而不使用本机渲染器

时间:2016-04-05 08:04:42

标签: listview xamarin.forms

有没有办法在没有自定义渲染器的情况下自定义Xamarin.Forms ListView(或Cell)?

我发现的每个结果都使用原生渲染器解释,但它不是我想要的。

我只想从我的共享项目PCL自定义ListView或ListView的单元格,以保持Xamarin.Forms的力量,并在应用程序之间共享每个UI代码。

编辑1:

public class TappticCell : ViewCell
{
    #region BindableProperty Fields

    public static readonly BindableProperty NameProperty =
        BindableProperty.Create("Name", typeof(string), typeof(string), "");

    public static readonly BindableProperty NameColorProperty =
        BindableProperty.Create("NameColor", typeof(Color), typeof(Color), Color.Transparent);

    #endregion

    #region BindableProperty Properties

    public string Name
    {
        get { return (string)GetValue(NameProperty); }
        set { SetValue(NameProperty, value); }
    }

    public Color NameColor
    {
        get { return (Color)GetValue(NameColorProperty); }
        set { SetValue(NameColorProperty, value); }
    }

    #endregion

    #region Fields

    private readonly Label _nameLabel;
    private readonly StackLayout _stackLayout;

    #endregion

    #region Constructors

    public TappticCell()
    {
        [...]
        _nameLabel = new Label { HorizontalOptions = LayoutOptions.CenterAndExpand };

        _nameLabel.SetBinding(Label.TextProperty, "Name");
        _nameLabel.SetBinding(Label.TextColorProperty, "NameColor");

        [...]
    }

    #endregion
}

我的问题是第二个带有BindableProperty NameBolor的SetBinding不起作用......这里我在XAML中使用

<cells:TappticCell Image="{Binding Image}"
                           Name="{Binding Name}"
                           BackgroundColor="Gray"
                           NameColor="{Binding Favorite, Converter={StaticResource BooleanToColor}}"
                           TextSize="30" />

当我在ImageCell上使用它时,效果很好,但不适用于我的自定义单元格...

由于

2 个答案:

答案 0 :(得分:0)

如果我清楚地了解你,你想要定制细胞的外观。 在这种情况下,我建议使用viewCell来显示UI内容。 这是你如何编写viewCell类

public class EffortMenuCell:ViewCell
    {
        static int i = 0;
        public EffortMenuCell() : base() {

            var newLabel2 = new Label () {VerticalOptions = LayoutOptions.CenterAndExpand };


            newLabel2.SetBinding (Label.TextProperty, "nameLabel");
            var time = new Label ();
            time.HorizontalOptions = LayoutOptions.EndAndExpand;
            time.VerticalOptions = LayoutOptions.CenterAndExpand;
            time.SetBinding (Label.TextProperty, "timespan");
            var tempLayout2 = new StackLayout ();

            //tempLayout2.BackgroundColor = Color.White;
            tempLayout2.Padding = new Thickness(20,0,20,0);
            tempLayout2.Spacing = 20;

            tempLayout2.Orientation = StackOrientation.Horizontal;

// based on index Different Row appearance ,

            if (i == 0) {
                tempLayout2.BackgroundColor = Color.FromHex ("e0e0e0");
                newLabel2.TextColor = Color.FromHex ("a4a4a4");
                time.TextColor = Color.FromHex ("01bcbc");
                this.IsEnabled = false;
                i++;
            } else {
                if (i <= 4) {
                    tempLayout2.BackgroundColor = Color.White;
                    newLabel2.TextColor = Color.Black;
                    time.TextColor = Color.FromHex ("007fbf");
                    i++;
                }
                if (i == 5) {
                    i = 0;
                }
            }

            tempLayout2.Children.Add (newLabel2);
            tempLayout2.Children.Add (time);
            this.View = tempLayout2;
        }
    }

答案 1 :(得分:0)

Effects可能是您的选择。

  

效果允许自定义每个平台上的本机控件,并且通常用于小样式更改。本文介绍了效果,概述了效果和自定义渲染器之间的界限,并描述了PlatformEffect类。

如果您想完全控制listview,仍然需要渲染器。