A note on Singleton vs Transient
Note: A common misconception is that using Singleton Pages consumes vast amounts of memory, because after all, a fully realised ContentPage can have a large footprint. In Maui, cross-platform controls are lightweight wrappers around platform controls, and these controls are only instantiated when they are part of the visual tree.
ContentPage can have a large footprint. In Maui, cross-platform controls are lightweight wrappers around platform controls, and these controls are only instantiated when they are part of the visual tree.We use Singleton for our Pages and PageViewModels where we can. The exceptions are:
DetailPage
Two instances of
DetailPagecan exist, one on each navigation stack.Mauidoes not allow the samePageinstance to be present in the visual tree in two places, so we register thisPageasTransient, meaning a new one is created each time we push aPage
DetailPageVm
Because we store a different message in our
ViewModeldepending on what pushed thePage, we need oneViewModelperPageinstance, so we register theDetailPageVmasTransient
Try swapping DetailPageVm to Singleton and see if you can see why that's not right in this case.
Last updated