【问题标题】:Custom Xamarin Cell Issue自定义 Xamarin 单元问题
【发布时间】:2015-09-09 16:16:09
【问题描述】:

我正在学习使用 Xamarin,并且正在制作一个简单的自定义单元格。但是,当我运行应用程序时,我设置为 ListViews ItemsSource 的任何信息都不会显示。我想知道我绑定信息的方式是否存在问题,或者我构建自定义单元格的方式是否存在问题。

这是单元格类:

public class ButtonCell : ViewCell
{
    #region Constructors
    public ButtonCell()
    {
        //Button bg = new Button();
        Label title = new Label()
        {
            TextColor = Color.Black,
            FontSize = 12,
            YAlign = TextAlignment.Start
        };

        Label description = new Label()
        {
            TextColor = Color.Black,
            FontSize = 12,
            YAlign = TextAlignment.End
        };

        title.SetBinding(Label.TextProperty, new Binding("Title"));
        description.SetBinding(Label.TextProperty, new Binding("Budget"));

        Grid labelLayout = new Grid()
        {
            /*VerticalOptions = LayoutOptions.Center,*/
            Padding = new Thickness(5, 0, 5, 10),
            Children = 
            {
                title, 
                description
            }
        };

        View = labelLayout;

        /*Grid grid = new Grid()
        {
            Padding = new Thickness(5, 0, 5, 10),
            Children = 
            {
                bg,
                labelLayout
            }
        };*/
    }
    #endregion
}

这是我想在列表视图中显示信息的类:

public class Bucket
{
    #region Public Variables
    public string Title;
    public float Budget;
    public BucketType Type;
    public BucketCategory Category;
    #endregion

    #region Constructors
    public Bucket()
    {
        Title = "";
        Budget = 0;
        Type = (BucketType)0;
        Category = (BucketCategory)0;
    }

    public Bucket(string title, float budget, BucketType type, BucketCategory category)
    {
        Title = title;
        Budget = budget;
        Type = type;
        Category = category;
    }
    #endregion
}

public enum BucketType
{
    Flexible = 0,
    Fixed
}

public enum BucketCategory
{
    Bills = 0,
    Food,
    Hobbies
}

当我初始化列表视图时,它会显示适当数量的单元格。但是,没有显示任何信息。再一次,我不确定它是绑定问题还是单元格的格式问题。

提前感谢您的帮助!

【问题讨论】:

  • 您的可绑定值(Title、Budget 等)必须是公共属性,而不仅仅是公共成员变量

标签: mobile xamarin cross-platform


【解决方案1】:

Bucket类中以下成员变量需要修改为属性:

#region Public Variables
public string Title;
public float Budget;
public BucketType Type;
public BucketCategory Category;
#endregion

需要更改为:

#region Public Variables
public string Title {get;set;};
public float Budget{get;set;};
public BucketType Type{get;set;};
public BucketCategory Category{get;set;};
#endregion

您还需要实现 IPropertyChanged 才能使绑定成为 OneWay 以外的任何东西。我使用了一个名为 Fody.PropertyChanged 的​​ nugget 包,但具体实现取决于您。

【讨论】:

  • 太棒了!感谢您的帮助!
猜你喜欢
  • 2020-05-29
  • 2017-05-21
  • 2011-04-22
  • 1970-01-01
  • 1970-01-01
  • 2021-04-04
  • 2020-10-04
  • 1970-01-01
  • 2019-05-06
相关资源
最近更新 更多