Page Building and Cleanup (Day 3)

Most navigation service helpers or code I have come across just construct a new instance of a page and pass it to the stack, normally within the navigation service. I prefer to implement a factory pattern to build the page as it lets you control the page to finer degree and you can swap out page building factories within your code if necessary.

In my Page Building Service I do 3 things

  1. Map a page type to a view model type
  2. Construct the Page
  3. Locate the ViewModel and set to the pages BindingContext

An example of my PageService.cs

ViewModel to Page Mapping

Normally a ViewModel is set to the BindingContext within the page code behind.

But the traditional approach has 2 major cons.

  1. Your page has code behind to set the binding context.
  2. Your ViewModels as static references publically accessible around the application.

A new approach as shown in the page service allows

  1. BindingContext is set when the page is constructed in a central location
  2. No code behind is needed on the View, keeping it squeaky clean
  3. Mappings between Page and ViewModel are defined in one location and not scattered around.


When I create my Navigation Stack (coming up later) – you will see the MapViewModel method. This is where I map a page type to a view model. The great thing about this is you can even reuse Views and ViewModels if needed as neither of them ever have a reference on each other.

Page Cleanup

Extending on managing the page construction, we can manage the page destrucstion as well. At point we need to create a new NavigationPage. An example is ExtNavigationPage.cs

In this, I manage the OnPopped event, and in each page, we reference a cleanup event. The cleanup event is just a virtual method in a common base page inherited by all the views. While Xamarin Forms is meant to clean up everything, I find at times it fails to wipe everything, hence why I explicity set it.

While each page can do any further cleanup, here I make sure that the BindingContext, Toolbars and Content are all set to null.

5 Common Pitfalls In Enterprise Mobile Development

Based on my experience, of over 9,000hrs of Xamarin.Forms development, check out some of my hard learnt lessons, in enterprise Xamarin.Forms development.

<< Navigation Service (Day 2) || The ViewModel (Day 4) >>

14 Days To Building An Enterprise Quality Xamarin Forms App


Subscribe to a hand-picked round up of the best Xamarin development links every week. Published every Friday. Free.

Signup to Weekly Xamarin

* I do not maintain or run this newsletter, but I do recommend it.


  1. Mathew

    Can i use this method to do something every time one page ins this stack use do the opoasync() by her own?

    1. Adam Pedley

      Not sure I understand the question completely. You would put this code, based off the NavigationPage, so it would run for any page that is popped from that NavigationPage.

      1. Mathew

        Something like this “” but in a detailpage context.