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
  3. Configuring your root Page

Root ContentPage

This is by far the simplest, and the least flexible. It restricts us to having a single ContentPage for the entire application.

The objective here is quite simply to get a reference to a suitable ContentPage and assign it to App.MainPage in the App constructor, so if you're a bit of a maverick you can just new up your ContentPage, set its BindingContext to something, and make the assignment, something like this:

var rootPage = new MyRootPage();
var vm = new MyRootPageVm();
vm.Init(/* ... */);
rootPage.BindingContext = vm;
MainPage = rootPage;

What if your MyRootPage or (more likely) your MyRootPageVmhas dependencies? Things can start to turn ugly, so let's do this using IoC ...

Once you have added your Page and ViewModel to the IoC container (in MauiProgram.cs), you can do this:

public partial class App : Application
{
    public App(IPageServiceZero pageService)
    {
        InitializeComponent();

        // Don't forget to call pageService.Init, or navigation will not work properly!
        pageService.Init(this);
            
        var mvvmPage = pageService.GetMvvmPage<MyRootPage, MyRootPageVm>();
        var mvvmPage.viewModel.Init(/* ... */);
        MainPage = mvvmPage.page;
    }
}

Simple.

PreviousConfiguring your root PageNextRoot NavigationPage

Last updated 1 year ago