MessagingCenter used for Navigation, when to subscribe/unsubscribe messages

PatrickJelittoPatrickJelitto Member ✭✭✭

Hi Guys,

i have the following NavigationStack:

MainPage
OverviewPage
VehicleStatePage
ImagePage

VehicleStatePage is a galery of images. You can tap the images and navigate into a ImagePage. The event is in the code behind of VehicleStatepage.
You can also go from ImagePage to the next ImagePage. If you do so I send a message from ImagePage to the code behind of VehicleStatePage on order to simulate the same behaviour like the user tapped this specific image from VehicleStatePage. So navigating from VehicleStatePage to ImagePage uses the same event like navigating from ImagePage to ImagePage.

Now i was wondering where is the best place to subscribe/unsubscribe to the message to avoid multiple subscriptions.

Thank you, Gelatto

Answers

  • seanydaseanyda GBMember ✭✭✭✭✭

    I don't think you should be using MessagingCenter at all for what you're trying to do. Why can you not just use Navigation.PushAsync and Navigation.PopAsync()?

    If you're using it to clear the page stack you can use Navigation.RemovePage();

  • PatrickJelittoPatrickJelitto Member ✭✭✭

    I wanted to avoid to implement the same code in different files. Because i have different Types of ImagePages and also different ViewModels for those. I thought a central function would be the best. I could also see what my mistake was and i got it running now.

Sign In or Register to comment.