0

I have a Xamarin project in which I have a Cart Page. I'm trying to update the data whenever I click on an add or remove button but it doesn't. Here's the code

public class CartViewModel : BindableObject
    {
.... 

private ObservableCollection<OrderDisplay> _ordersList;
        public ObservableCollection<OrderDisplay> OrdersList
        {
            get => _ordersList;
            set
            {
                _ordersList = value;
                OnPropertyChanged();
            }
        }

And then I have AddTotal where I try to update it. It is called when pressing the add or remove button

 private async void AddTotal(OrderDisplay oCart, int quantity)
        {
            decimal total = 0;
            int index = 0;

            if (oCart != null)
            {
                foreach (OrderDisplay obj in OrdersList)
                {
                    if (obj.Id == oCart.Id)
                    {
                        break;
                    }
                    index += 1;
                }
                OrdersList[index].Quantity = quantity;
                OrdersList[index].Total = quantity * OrdersList[index].Price;
                //OrdersList = null;
                //OrdersList = tempOrdersList;

                var order = await _apiService.GetOrderById(OrdersList[index].Id, token);
                order.Data.Quantity = OrdersList[index].Quantity;
                var orderUpdated = await _apiService.UpdateOrder(Convert.ToString(order.Data.Id), token, order.Data);

                if (!orderUpdated)
                {
                    await _messageService.ShowMessageAsync("Error", "Ha ocurrido un error.", "Ok", "");
                    return;
                }
            }

            foreach (OrderDisplay order in OrdersList)
            {
                total = order.Total + total;
            }

            LblTotalCart = string.Format("{0:N2}€", total);

        }

For context here is the view

enter image description here

I don't know how to do it. Please help.

EDIT

I tried doing it with INotifyPropertyChanged but gives me NullReference. I don't know if this is correct

public class OrderDisplay : INotifyPropertyChanged
    {
        //public int Id { get; set; }
        //public int Quantity { get; set; }
        //public decimal Price { get; set; }
        //public decimal Total { get; set; }
        //public CrProduct Product { get; set; }

        private int id;
        public int Id
        {
            get { return id; }
            set
            {
                id = value;
                OnPropertyChanged("Id");
            }
        }

        public int quantity;
        public int Quantity
        {
            get { return quantity; }
            set
            {
                quantity = value;
                OnPropertyChanged("Quantity");
            }
        }

        public decimal price;
        public decimal Price
        {
            get { return price; }
            set
            {
                price = value;
                OnPropertyChanged("Price");
            }
        }

        public decimal total;
        public decimal Total
        {
            get { return total; }
            set
            {
                total = value;
                OnPropertyChanged("Total");
            }
        }

        public CrProduct product;
        public CrProduct Product
        {
            get { return product; }
            set
            {
                product = value;
                OnPropertyChanged("Product");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
robluc
  • 121
  • 12
  • "doesn't work" is not a useful description of the problem. If you are saying that the changes to individual `OrderDisplay` properties are not reflected in the UI, then the likely cause is that `OrderDisplay` needs to implement `INotifyPropertyChanged` – Jason Jun 14 '22 at 11:27
  • @Jason I already tried that as I say in my question but the changes aren't visible either way – robluc Jun 14 '22 at 11:29
  • It would be great to see full example of code. "click on an add or remove button but it doesn't" I think should be some code that adds or remove from `OrdersList`, without it the ListView will not be updated – choper Jun 14 '22 at 11:37
  • @choper The method `AddTotal` is the one that I call when trying to update it. I'm trying to update the items inside of that list (price and quantity). Adding an item or removing it works fine. – robluc Jun 14 '22 at 11:41
  • you tried it on `CartViewModel`, NOT `OrderDisplay`. You are modifying properties of `OrderDisplay`, therefore that is where you need to add INPC. – Jason Jun 14 '22 at 11:41
  • @Jason `OrderDisplay` is just the model fot the items that I'm showing in my cart. The names may be confusing – robluc Jun 14 '22 at 11:42
  • does `OrderDisplay` contain the properties for `Price` and `Quantify`? – Jason Jun 14 '22 at 11:44
  • and does it implement `INotifyPropertyChanged`? – choper Jun 14 '22 at 11:45
  • @Jason `OrderDisplay` is my model not a ViewModel. I don't know if I should implement INotifyPropertyChanged on the model itself – robluc Jun 14 '22 at 11:48
  • That is the source of your problem, as I have pointed out twice. How you choose to solve it is up to you. – Jason Jun 14 '22 at 11:51
  • @choper I implemented `INotifyPropertyChanged` on my ViewModel not the `OrderDisplay` model, should I do that? – robluc Jun 14 '22 at 11:52
  • @Jason I don't know how to solve it that's why I asked – robluc Jun 14 '22 at 11:53
  • 1
    implement `INotifyPropertyChanged` on `OrderDisplay`. Now that is the third time I've suggested that. – Jason Jun 14 '22 at 11:54
  • @robluc when you use Binding, to notify View about changes you need to use PropertyChanged event, with out it View will never know about any changes in you Model or ViewModel – choper Jun 14 '22 at 12:12

1 Answers1

1

INotifyPropertyChanged is the mechanism that the UI uses to determine when it needs to update a bound property. If you are changing property P on class X, then X needs to implement INotifyPropertyChanged and raise a PropertyChanged event when P is modified.

an ObservableCollection<T> only raises events when items are removed or added from the collection. It does not raise events when individual properties on the class T are modified.

Jason
  • 86,222
  • 15
  • 131
  • 146
  • I added this to my `OrderDisplay` model but it gives me `NullReference`. I'll update the code – robluc Jun 14 '22 at 15:14
  • Which specific line? What is null? A null ref is a very common C# exception and there are some pretty basic debugging steps you should do when you have one. – Jason Jun 14 '22 at 15:18
  • Already fixed the issue. I could achieve what I wanted. Thanks. – robluc Jun 14 '22 at 15:22