UILabel在RaisePropertyChanged之后不会更新

时间:2017-02-22 17:20:41

标签: data-binding xamarin.ios mvvmcross

我想要实现的目标

我有一个视图,它有一个选择器和一个子视图。当我更改选择器的值时,子视图中的某些值应该更改。

问题

当我更改选择器的值时,变量One和Two应该更新,但更改不会显示在视图上。

代码

我有一个视图,我绑定了几个属性,View.cs

public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        this.tblCollection.TableFooterView = new UIView(CGRect.Empty);

        var source = new CollectionSource(this.tblCollection);

        var set = this.CreateBindingSet<ItemView, ItemViewModel>();
        set.Bind(source).To(vm => vm.Item.ObsCollection);
        set.Bind(lblOne).To(vm => vm.Item.One);
        set.Bind(lblTwo).To(vm => vm.Item.Two);
        set.Bind(lblThree).To(vm => vm.Item.Three);
        set.Apply();

        this.tblCollection.Source = source;

        this.tblCollection.ReloadData();

    }

此视图位于另一个视图,即FatherView.cs,它有一个选择器

private void OnSyncFinished(SyncComplete obj)
    {
        this.InitPicker(this.PickerDiscount,
            this.btnDiscount,
            this.ViewModel.Discounts.Select(m => m.Desc).ToList(),
            "Discount");

        this.ViewModel.Discount = this.ViewModel.Discounts.FirstOrDefault();
    }


    void InitPicker(ModalPickerViewController picker, UIButton btn, List<string> list, string title)
    {
        if (picker == null)
        {
            picker = this.CreateCustomPicker(title, list, UIUtils.ColorGreen);
            picker.OnModalPickerDismissed += (sender, args) => this.OnElementSelected(picker);
            btn.TouchUpInside += (sender, args) => PresentViewControllerAsync(picker, true);
        }
        else
        {
            picker.PickerView.Model = new CustomPickerModel(list);
        }
    }

    void OnElementSelected(ModalPickerViewController picker)
    {
        var index = picker.PickerView.SelectedRowInComponent(0);
        var discount = this.ViewModel.Discounts[(int) index];
        this.ViewModel.DiscountSelected = discount;
    }

当我修改DiscountSelected

    private Discount discountSelected;
    public Discount DiscountSelected
    {
        get { return this.discountSelected; }
        set
        {
            this.discountSelected = value;
            this.OnDiscountSelected(this.discountSelected);
            this.RaisePropertyChanged(() => this.DiscountSelected);
        }
    }

转到

public void OnDiscountSelected(Discount discount)
    {
        foreach (var item in ViewModelsCollection)
        {
            item.ItemSelected.Discount = discount.Total;
        }
    }

归于此

private int discount;
[XmlIgnore]
public int Discount
{
    get
    {
        return this.discount;
    }
    set
    {
        this.descuento = value;
        this.RaisePropertyChanged(() => this.Discount);
        this.RaisePropertyChanged(() => this.One);
        this.RaisePropertyChanged(() => this.Two);
    }
}

这个。一个和这个。两个得到更新。喜欢这个

    private decimal one;
    public decimal One
    {
        get
        {
            var dto = this.one * this.Discount / 100;

            return this.one - dto;
        }
        set
        {
            this.one = value;
            this.RaisePropertyChanged(() => this.One);
        }
    }

    private decimal two;
    public decimal Two
    {
        get
        {
            var dto = this.two * this.Discount / 100;

            return this.two - dto;
        }
        set
        {
            this.two = value;
            this.RaisePropertyChanged(() => this.Two);
        }
    }

0 个答案:

没有答案