如何将Label绑定到Xamarin中的属性?

时间:2016-11-08 18:47:08

标签: c# xamarin binding properties label

我有一个内容页面" AnimalPage "带标签。我想将labels.Text绑定到类的属性,以便在属性值更改时自动更新标签。

课程是" 动物"它有两个属性,周长&的长度即可。当任何一个值被修改时,第三个属性" 权重"自动计算(注意:触发计算的代码未在下面显示)。当权重属性发生更改时,我希望内容页面上的权重标签自动更新。

我在Xamarin上发现的许多例子都是我不使用的XAML。

目前,当页面加载时,初始值会显示在权重标签中,因此看起来绑定是正确的,但是当权重属性更改时,标签不会更新。

我在代码中放置了断点,并且正在调用calcWeight方法并且weight属性正在改变,但weightCell.cellText不会改变。

我错过了什么?

public class Animal {
    public string Name { get; set; }

    private double _girth;
    // when girth changes, save the value and trigger a re-calculation of weight
    public double girth { get { return _girth; } set { _girth = value; this.calcWeight(); } }
    private double _length;

    // same for length changes; save the value and trigger a re-calculation of weight
    public double length { get { return _length; } set { _length = value; this.calcWeight(); } }

    private double _weight;
    public double weight { get { return _weight; } set { _weight = value; } }

    public Animal()
    {
        ...
    }
    ...
    public double calcWeight()
    {
        // formula for weight calculation goes here...
        ...
        this.weight = weight;
        return weight;

    }
}

显示此课程的页面如下:

internal class AnimalPage : ContentPage
{
    private Animal animal { get; set; }

    public AnimalPage(Animal animal)
    {
        this.animal = animal;
        BindingContext = this.animal;
        var weightCell = new ResultCell(); // ResultCell is a custom ViewCell
        Binding myBinding = new Binding("weight");
        myBinding.Source = this.animal;
        weightCell.cellText.SetBinding(Label.TextProperty, myBinding);
        ...
    }
}

为了完整起见,这里是ResultCell类,它只是一个自定义的ViewCell,水平显示两个标签。

public class ResultCell : ViewCell {
    public Label cellLabel, cellText;

    public ResultCell() {
        cellLabel = new Label();
        cellText = new Label();

        var cellWrapper = new StackLayout {
            ...
            Children = { cellLabel, cellText }
        };
        View = cellWrapper;
    }
}

2 个答案:

答案 0 :(得分:0)

如果您希望UI在数据更改时自动更新,您的Animal类需要实现INotifyPropertyChanged,并且您需要在修改或重新计算权重属性时触发PropertyChanged事件。此事件会警告UI需要刷新。

答案 1 :(得分:0)

因为这花了我很长时间才弄清楚我以为我会发布更正后的动物类,以防其他人帮助。

public class Animal : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public string Name { get; set; }

    private double _girth;
    public double girth { get { return _girth; } set { _girth = value; this.calcWeight(); } }

    private double _length;
    public double length { get { return _length; } set { _length = value; this.calcWeight(); } }

    private double _weight;
    public double weight { get { return _weight; } set { _weight = value; NotifyPropertyChanged(); } }

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

public Animal()
{
    ...
}
...
public double calcWeight()
{
    // formula for weight calculation goes here...
    ...
    this.weight = weight;
    return weight;

}
}