Forum Xamarin.Forms

I cant refresh my listview with new data in my UI when I change something. I'm using MVVM

I got a TabbedPage, this contain two pages (Products - ShoppingCart). To both of them I shared in their constructor the same instant of my viewmodel. In my fisrt page a want to search some products and add it to my second page (my shopping cart) every time I chose a product from the first page I add it on a "list" and I want that the second page could read that list and refresh the UI with the items of the list and if I add the same product I will check on the list and if it exist I'll change the count and all of that I need to reflect it in the second page when that process finish.

Best Answer

  • matrixlukanmatrixlukan Member ✭✭✭
    Accepted Answer

    Define your **CartItem **class as follows.
    public class CartItem
    {
    public Product Item {get;set;}
    public int ItemCount {get;set;} = 1;
    }

    Now change AddItemToCart() as follows:
    public void AddItemToCart(Product newProduct)
    {
    var cartItems = Cart.ToList();
    if(cartItems.Exists(x=>x.Id == newProduct.Id))
    {
    cartItems.FirstOrDefault(x=>x.Id == newProduct.Id).ItemCount ++;
    }
    else
    {
    cartItems.Add(newItem);
    }

    Cart = new ObservableCollection<CartItems>(cartItems);
    OnPropertyChanged(Cart);
    

    }

    If this solves your issue then hit a like.

Answers

  • matrixlukanmatrixlukan Member ✭✭✭
        public ObservableCollection<CartItems> Cart {get; set; } = new  ObservableCollection<CartItems>();
        ....
        ....
    
        public void AddItemToCart(CartItem newItem)
        {
            var cartItems = Cart.ToList();
            cartItems.Add(newItem);
            Cart = new ObservableCollection<CartItems>(cartItems);
            OnPropertyChanged(Cart);
        }
    

    I hope this solves your problem.

  • SpontonLucianoSpontonLuciano Member ✭✭✭

    @matrixlukan Thats worked to add an item! How can I change the "count" property of a product which is already on the list and then show it again with the new count on the UI?

  • ColeXColeX Member, Xamarin Team Xamurai
    edited November 2019

    Make your Product class implement INotifyPropertyChanged , create binding between Count and Label.Text , notify the UI while Count property changes .

     public class Product : INotifyPropertyChanged
     {
        private int count;
        // Declare the event
        public event PropertyChangedEventHandler PropertyChanged;
    
        public Product()
        {
        }
    
        public string Count
        {
            get { return count; }
            set
            {
                count = value;
                // Call OnPropertyChanged whenever the property is updated
                OnPropertyChanged("Count");
            }
        }
    
        // Create the OnPropertyChanged method to raise the event
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
    
  • matrixlukanmatrixlukan Member ✭✭✭
    Accepted Answer

    Define your **CartItem **class as follows.
    public class CartItem
    {
    public Product Item {get;set;}
    public int ItemCount {get;set;} = 1;
    }

    Now change AddItemToCart() as follows:
    public void AddItemToCart(Product newProduct)
    {
    var cartItems = Cart.ToList();
    if(cartItems.Exists(x=>x.Id == newProduct.Id))
    {
    cartItems.FirstOrDefault(x=>x.Id == newProduct.Id).ItemCount ++;
    }
    else
    {
    cartItems.Add(newItem);
    }

    Cart = new ObservableCollection<CartItems>(cartItems);
    OnPropertyChanged(Cart);
    

    }

    If this solves your issue then hit a like.

  • SpontonLucianoSpontonLuciano Member ✭✭✭

    @matrixlukan Thanks!!

Sign In or Register to comment.