Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Messaging center

AlbertoCAlbertoC ITMember ✭✭

Hi Guys,

I try to to use messaging center from platfomr andorid to .net standard project.

The messagin center send the message one time but the receiver is activated 2 times.

I have an andorid device version 5.1, i dont' know if this colud be usefull.

Here the piece od code od the receiver

MessagingCenter.Subscribe<string, string>("iMioDocPdaEntry.Android", "BARCODE_RECEIVED", async (sender, arg) =>
{
{
if (DocRigheView.attiva)
{
await RicercaArtBarCode(arg);
}
else
{
await RicercaCommBarCode(arg);
}
}
});

her bolwo ther sender

MessagingCenter.Send<string, string>("iMioDocPdaEntry.Android", "BARCODE_RECEIVED", result);

Posts

  • RavinderJangraRavinderJangra USMember ✭✭✭

    @AlbertoC please unsubscribe to messagecenter on ondisappearing

  • AlbertoCAlbertoC ITMember ✭✭

    but if i send the messagge one time, and in the debug mode i see that the subscribe is called two times also if i suscribe the messagge i dont' solve the porblem

    the problem is that the subscribe is called two times

  • NMackayNMackay GBInsider, University admin
    edited January 2018

    The safest way is to register in the page/class constructor and unregister for example when the page is popped of the navigation stack, there's a popped event which handles this.

  • AlbertoCAlbertoC ITMember ✭✭

    I insert the unsubscribe in the viemodel, becuase i there i do some calculation that cannot be done again, but i appreciate both answers

  • NMackayNMackay GBInsider, University admin
    edited January 2018

    @AlbertoC

    Yeah, same approach in the VM, when the VM is going to be disposed I have a cleanup that unregisters messages etc. You can implement a common interface and when the page is popped call the VM cleanup before resetting the bindingcontext, frameworks like Prism make this very easy with IDestructible, very handy.

Sign In or Register to comment.