How to Dispose a ViewModel after Popping a page with Xamarin.Forms?

You can make sure the Dispose() is called in the OnDisappearing() event of the View, if you want to ensure that ViewModel is not present anymore in the memory than the view.

It is better if you care only about subscribe and unsubscribe of events, then to do it in the OnAppearing() and OnDisappearing(). In that case you will be sure of no event handlers been present on the viewmodel once view is not visible.


Implement IDestructible or INavigationAware

(or both as in BaseViewModelsample from Prism).


Depending on your object lifecycle :

  • Implement your disposal code in the Destroy method of IDestructible interface.
  • Implement your disappearing/appearing code in the OnNavigatedFrom/OnNavigatedTo methods on INavigationAware interface.

Bonus: IDestructible can be implemented also by the View (and it will called accordingly by Prism when the view is destroyed).

Note:
While the solution above using OnAppearing/OnDisappearing works, it induces that ViewModel will depends on a call from View for managing its lifecycle (not clean). Moreover these methods don't exist on ContentView.