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
DetailPage
can exist, one on each navigation stack.Maui
does not allow the samePage
instance to be present in the visual tree in two places, so we register thisPage
asTransient
, meaning a new one is created each time we push aPage
DetailPageVm
Because we store a different message in our
ViewModel
depending on what pushed thePage
, we need oneViewModel
perPage
instance, so we register theDetailPageVm
asTransient
Try swapping DetailPageVm
to Singleton
and see if you can see why that's not right in this case.
Last updated