Windows Phone - Removing pages from the back stack with Caliburn Micro

12 February 2012

It’s quite a common scenario to have a page from which the user is navigated (login page or purchase page) but when the user leaves that page you don’t want them going back there via the back button.

You can handle this quite easily in code behind via the OnNavigatedTo override but in Caliburn Micro it gets even neater.

Firstly, in your bootstrapper class for Caliburn add the following to the root frame’s navigated handler. We are getting the navigation service from the DI container and if a particular querstring value was on the target page, we remove the source page from the back stack.

void RootFrameNavigated(object sender, NavigationEventArgs e)
    var navService = GetInstance(typeof(INavigationService), null) as INavigationService;
    if (e.NavigationMode == System.Windows.Navigation.NavigationMode.New && e.Uri.ToString().Contains("BackNavSkipOne=True"))

On your base view model (you do have one right?) add the following property.

/// <summary>
/// Removes the source page from the back stack
/// </summary>
public bool BackNavSkipOne {get;set;}

Then lastly, when you want to navigate away from a page you don’t want the user to come back to, simply make sure BackNavSkipOne is true!

NavigationService.UriFor<YourViewModel>().WithParam(vm => vm.BackNavSkipOne, true).Navigate();

This is a nice transparent way to handle the back stack with Caliburn Micro.

Want to get started?

We would love to help with your next app or game, please do get in touch.