FunctionZero
  • Libraries
    • Maui.MvvmZero
      • Overview
      • Quickstart
      • Walkthrough
        • Create your application
        • Create your ContentPages
        • Create your ViewModels
        • Register your new content
        • Launching our app!
        • Adding Navigation
        • Summary
      • Recommended naming conventions
      • Configuring your root Page
        • Root ContentPage
        • Root NavigationPage
        • Root TabbedPage
        • Root FlyoutPage
      • A note on Singleton vs Transient
      • Recommended base classes for PageViewModels and ViewModels
    • Maui.Controls
      • ExpanderZero
      • FocusScrollZero
      • LabelZero
      • ListViewZero
      • MaskZero
      • MultiViewZero
      • TreeViewZero
    • CommandZero
    • Maui.BindZero
      • Quickstart
      • z:Bind
      • Examples
      • The Great Escape
      • Casting
      • Short Circuit
      • Errors
      • Aliases
      • Value types and Reference types
      • z:Function
      • z:TapTrigger
      • z:EdgeTrigger
      • z:Latch
      • Advanced Usage - Functions, aliases and operator-overloads
    • AnimationZero
    • LocalisationZero
Powered by GitBook
On this page
  1. Libraries
  2. Maui.MvvmZero

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.

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 same Page instance to be present in the visual tree in two places, so we register this Page as Transient, meaning a new one is created each time we push a Page

DetailPageVm

  • Because we store a different message in our ViewModel depending on what pushed the Page, we need one ViewModel per Page instance, so we register the DetailPageVm as Transient

Try swapping DetailPageVm to Singleton and see if you can see why that's not right in this case.

PreviousRoot FlyoutPageNextRecommended base classes for PageViewModels and ViewModels

Last updated 1 year ago