Forum Cross Platform with Xamarin

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Handling back navigation in MasterDetailPage with a NavigationPage

I'm working on a project where we have a navigation service that performs navigation using view models (similar to Rock Solid Knowledge's article about decoupling views and navigation, I would link but my account is too new for that). One of the main things that the navigation service does is notify the view models before and after navigation and allows view models to prevent navigation from occurring (if they are in the middle of an loading data, for example). This allows me to reload data when re-entering a view and unload data (if needed) when leaving a view. This all works great except for the back button that is shown when using a MasterDetailPage with a NavigationPage as it's Detail view.

What I want to know is if there is anyway to intercept that back button so I can force it to use my navigation service? I don't want to get rid of the back button as it's a useful feature, I just need to have it go through my navigation service.

Best Answer

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    This all works great except for the back button that is shown when using a MasterDetailPage with a NavigationPage as it's Detail view.

    What I want to know is if there is anyway to intercept that back button so I can force it to use my navigation service?

    Sorry, I don't quite follow you. How does this back button not work? Can you elaborate on the details?

    Could you please share your detailed steps and screenshots to help me understand the problem?

    Thanks.

  • amura11amura11 Member ✭✭

    Hey jezh,

    The back button works, the problem is that when the back button (not the hardware one, the software one) is pressed it directly pops items off the navigation stack. What I wanted was to have a way to make the software back button go through my service to pop items off the stack so that I could call methods on my ViewModels before and after navigating as well as prevent navigation.

    After some research I think I was approaching the problem from the wrong angle. First off, stopping navigation is apparently a bad idea and what I wanted was for my ViewModels to be able to know when they were navigated to and navigated away from or shows/hidden. What ended up being a better solution for me was to use the Page Appearing and Disappearing events. To get this to work I used a custom NavigationPage that would attach and detach event handlers for the Appearing and Disappearing events. The event handlers would get the binding context as my base ViewModel and call the appropriate method on the ViewModel (Shown/Hidden).

    I've attached the custom NavigationPage I ended up using

    TL;DR: What I was trying to do was dumb, what I really needed was a way for my ViewModels to know when they were displayed/hidden and I did that using the Appearing/Disappearing events and some glue code.

Sign In or Register to comment.