如何阻止重置属性

时间:2019-04-23 00:30:55

标签: c# xamarin.forms

在BaseViewModel上,我有一个名为OrderService的静态类,该类有一个订单集合,每个Order都有一个IFoodItems集合。当我调用AddOrder方法并传递顺序时,它会正确保存它,并且IFoodItems上的属性值是正确的。当我在不同的ViewModel上调用我的GetOrders方法时,IFoodItems上的quantity属性将重置为零,我不希望这种情况发生。如何保存其价值?

我尝试过使列表成为静态列表,在添加食物列表之前先捕获食物列表,然后使用MessageCenter类发送该列表。

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;
        }

订单类:

 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 个答案:

答案 0 :(得分:1)

每次您继承BaseViewModel并实例化后代时,都会调用基本构造函数并创建OrderService的新实例。

protected static OrderService OrderService = new OrderService();

并将其从构造函数中删除。