自定义 Xamarin 单元格问题

Custom Xamarin Cell Issue

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

这是单元格 class:

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
}

这是我要在列表视图中显示信息的class:

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
}

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

提前感谢您的帮助!

Bucketclass中的以下成员变量需要更改为属性:

#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

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