On my BaseViewModel I have a static class called OrderService that has a collection of orders and each Order has a collection of IFoodItems. When I call the AddOrder method and pass the order it saves it correctly and the values of the properties on the IFoodItems are correct. When i call my GetOrders method on a different ViewModel the Quantity property on the IFoodItems are reset to zero and i dont want that to happen. How can preserve its value?

I've tried making the list static, capturing the foodlist before adding it, and sending it using the MessageCenter class.

ViewModel:

class TakeOrderViewModel : BaseViewModel
    {
        #region Properties
        private double _orderPrice;
        ObservableCollection<IFoodItems> _FoodList; 

        public ObservableCollection<IFoodItems> FoodList {
            get { return _FoodList; }
            set {
                SetProperty<ObservableCollection<IFoodItems>>(ref _FoodList, value);
            }
        }
        public ICommand SaveOrderCommand { get; private set;}
        public double OrderPrice 
        {
            get { return _orderPrice; }
            set {
                SetProperty<double>(ref _orderPrice, value);
            }
        }
        #endregion

        public TakeOrderViewModel()
        {
            FoodList = menuService.getMenu();
            OrderPrice = 0;
            SaveOrderCommand = new Command<string>(SaveOrder);
        }

        public void SaveOrder(string name)
        {
            Order CustomerOrder = new Order(name);            
            if(IsOrderEmpty())
            {
                App.Current.MainPage.DisplayAlert("Error", "The order has no quantity.", "OK");
            }
            else
            {
                foreach (IFoodItems item in FoodList)
                {
                    if(item.Quantity > 0)
                    {
                        CustomerOrder.AddItem(item);
                    }

                }
                CustomerOrder.SetPrice(OrderPrice);
                //MessagingCenter.Send<TakeOrderViewModel, Order>(this, "orderSender", CustomerOrder);
                OrderService.AddOrder(CustomerOrder);
                App.Current.MainPage.DisplayAlert("Message", "Order has been saved", "OK");
                //ClearOrder();
            }            
        }
        public bool IsOrderEmpty()
        {
            double temp = 0;
            foreach (IFoodItems item in FoodList)
            {
                temp += item.Quantity;
            }
            if (temp == 0)
                return true;
            else
                return false;

        }              
        public void ClearOrder()
        {
            foreach(IFoodItems item in FoodList)
            {
                item.Quantity = 0;
            }
            OrderPrice = 0;
        }
    }

OrderService:

 public class OrderService
    {
        ObservableCollection<Order> OrderList;
        public OrderService()
        {
            OrderList = new ObservableCollection<Order>();
        }

        public void AddOrder(Order value)
        {
            //one of the ways i tried to preserve the values
            ObservableCollection<IFoodItems> foods = value.foodItems;
            OrderList.Add(new Order(foods,value));
            //OrderList.Add(value);
        }
        public ObservableCollection<Order> GetOrders()
        {
            return OrderList;
        }

Order class:

 public class Order
    {
        public string Name { get; set; }
        public ObservableCollection<IFoodItems> foodItems;
        public double Price { get; set; }
        public string PriceString { get; set; }

        public Order(string name)
        {
            Name = name;
            foodItems = new ObservableCollection<IFoodItems>();
        }
        public Order(ObservableCollection<IFoodItems> order, Order val)
        {
            this.Name = val.Name;
            this.foodItems = order;
            this.Price = val.Price;
            this.PriceString = val.PriceString;
        }
        public void SetPrice(double value)
        {
            Price = value;
            PriceString = "Price: " + Price + " tickets";
        }
        public void AddItem(IFoodItems food)
        {
            foodItems.Add(food);
        }
    }

IFoodItem:

public interface IFoodItems : INotifyPropertyChanged
    {
        int Quantity { get; set; }
        double Weight_lbs { get; }
        double TicketPrice { get; }
        string Name{ get; set; }

        double GetDollarsMade();
        double GetTicketPrice();
        double GetAmountUsed();
    }

BaseViewModel:

public class BaseViewModel : INotifyPropertyChanged
    {
        protected MenuService menuService;
        protected static OrderService OrderService;

        public BaseViewModel()
        {
            menuService = new MenuService();
            OrderService = new OrderService();
        }
        #region INotifyPropertyChanged
        protected bool SetProperty<T>(ref T backingStore, T value,
            [CallerMemberName]string propertyName = "",
            Action onChanged = null)
        {
            if (EqualityComparer<T>.Default.Equals(backingStore, value))
                return false;

            backingStore = value;
            onChanged?.Invoke();
            OnPropertyChanged(propertyName);
            return true;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            var changed = PropertyChanged;
            if (changed == null)
                return;

            changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }

1 Answers

1
JSteward On Best Solutions

Each time you inherit BaseViewModel and instantiate a descendant the base constructor is called and creates a new instance of OrderService.

protected static OrderService OrderService = new OrderService();

And remove it from the constructor.