Configure MauiProgram.cs and launch in App.xaml.cs
MauiProgram.cs
In MauiProgram.cs configure MvvmZero and your container.
namespace SampleTabbedApp
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
*******************************
*** CONFIGURE MVVMZERO HERE ***
*******************************
)
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
#if DEBUG
builder.Logging.AddDebug();
#endif
********************************
*** CONFIGURE CONTAINER HERE ***
********************************
return builder.Build();
}
}
}
Configure MvvmZero
.UseMauiApp<App>()
.UseMvvmZero(
serviceBuilder =>
{
// Configure MvvmZero ...
serviceBuilder
.MapVmToView<ReadyPageVm, ReadyPage>() // Visualise a ReadyPageVm in a ReadyPage
.MapVmToView<SteadyPageVm, SteadyPage>()
.MapVmToView<GoPageVm, GoPage>();
...
}
Configure your container
Register all your Pages and ViewModels in the Container.
MvvmZero registers IPageService and NavigationPage in the container for you.
Unless you override the TypeFactory everything else MvvmZero is asked to instantiate must be registered in the container.
This includes FlyoutPage and TabbedPage if you use them.
builder.Services
// The root page is supplied by the container!
// AdaptedTabbedPage Because https://github.com/dotnet/maui/issues/14572
.AddSingleton<MultiPage<Page>, AdaptedTabbedPage>()
.AddSingleton<ReadyPage>()
.AddSingleton<SteadyPage>()
.AddSingleton<GoPage>()
.AddSingleton<ReadyPageVm>()
.AddSingleton<SteadyPageVm>()
.AddSingleton<GoPageVm>()
// DetailPage/Vm are transient because there can be more than one on any navigation stack at any time.
.AddTransient<DetailPage>()
.AddTransient<DetailPageVm>()
;
App.xaml.cs
In App.xaml.cs
public partial class App : Application
{
public App(IPageServiceZero pageService)
{
InitializeComponent();
pageService.Init(this); // Required!
// This app has a TabbedPage containing 3 tabs at the root.
MainPage = pageService.GetMultiPage(VmInitializer, typeof(ReadyPageVm), typeof(SteadyPageVm), typeof(GoPageVm));
}
private bool VmInitializer(object viewModel)
{
if (viewModel is ReadyPageVm)
return false; // Do not wrap the ReadyPage in a NavigationPage.
return true;
}
}
To navigate from within a ViewModel use the IPageServiceZero implementation:
using FunctionZero.CommandZero;
using FunctionZero.Maui.MvvmZero;
using System.Windows.Input;
namespace MvvmZeroTutorial.Mvvm.PageViewModels
{
public class SteadyPageVm : BasePageVm
{
private readonly IPageServiceZero _pageService;
public ICommand PushDetailPageCommand { get; }
public SteadyPageVm(IPageServiceZero pageService)
{
_pageService = pageService;
PushDetailPageCommand = new CommandBuilder()
.AddGuard(this)
.SetName("Push Detail Page")
.SetExecuteAsync(PushDetailPageExecuteAsync)
.Build();
}
private async Task PushDetailPageExecuteAsync()
{
// Push a page and tell it what pushed it.
await _pageService.PushVmAsync<DetailPageVm>(vm => vm.Init("Initialise message from SteadyPageVm"));
}
}
}