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.
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
I hope this solves your problem.
@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?
Make your
Product
class implement INotifyPropertyChanged , create binding betweenCount
andLabel.Text
, notify the UI whileCount
property changes .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);
}
}
If this solves your issue then hit a like.
@matrixlukan Thanks!!