I'm currently trying to use Messaging Center to update the count of a shopping cart amount the message is sent with the correct data but is never received, and I'm not sure if my implementation is right (Never used it before) I'm trying to send data from a method to a view model to update a view from an Observable List Count here is my implementation
public class Panier : BaseViewModel
{
private ObservableCollection<SmartDevice> content;
public Panier()
{
this.content = new ObservableCollection<SmartDevice>();
}
public ObservableCollection<SmartDevice> GetContent() { return content; }
public void AddProduct(SmartDevice product)
{
this.content.Add(product);
int data = this.CountPanier();
MessagingCenter.Send(this, "update counter", data);
}
public int CountPanier()
{
return this.content.Count;
}
}
Here is my View Model receiving the data
internal class AppShellViewModel : BaseViewModel
{
private int counter = 0;
public int Counter
{
get { return counter; }
set
{
MessagingCenter.Subscribe<Panier, int>(this, "update counter", (x, data) =>
{
counter = x.CountPanier();
OnPropertyChanged();
});
}
}
}
CodePudding user response:
first, you need to call Subscribe
in your VM constructor, so that it will start listening for any incoming messages
MessagingCenter.Subscribe<Panier, int>(this, "update counter", (x) =>
{
Counter = x;
});
then your send needs to use the same signature as the subscribe
MessagingCenter.Send<Panier,int>(this, "update counter", data);