I'm experiencing an annoying issue with xamarin messaging center. I added some break points and noticed that only one message is sent but the subscriber receives it twice.
My sender code(Page2):
async void CompartilhaMapa(System.Object sender, System.EventArgs e)
{
...
MessagingCenter.Send<Page2, ParamType>(this, "PopUpData", new ParamType() { Tipo = 2, Valor = Coords });
Console.WriteLine("msg sent"); //displayed only once and line above has a break line assuring this
await Navigation.PopPopupAsync();
}
Page1(Subscriber):
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Subscribe<Page2, ParamType>(this, "PopUpData", async (sender, arg) =>
{
Task.WaitAll(tasks.ToArray());
switch (arg.Tipo)
{
case 2:
Console.WriteLine(" *********** msg received"); //this is outputted twice
tasks.Add(Task.Run(() => ShareMap(arg))); //this is called twice
break;
case 3: tasks.Add(Task.Run(() => ShareEvent(arg))); break;
case 6: tasks.Add(Task.Run(() => ShareImage(arg))); break;
}
});
}
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Unsubscribe<Page2, ParamType>(this, "PopUpData");
}
What I'm doing wrong here?