FunctionZero.Maui.MvvmZero 2.0.0-pre1

This is a prerelease version of FunctionZero.Maui.MvvmZero.
There is a newer version of this package available.
See the version list below for details.
dotnet add package FunctionZero.Maui.MvvmZero --version 2.0.0-pre1
NuGet\Install-Package FunctionZero.Maui.MvvmZero -Version 2.0.0-pre1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FunctionZero.Maui.MvvmZero" Version="2.0.0-pre1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FunctionZero.Maui.MvvmZero --version 2.0.0-pre1
#r "nuget: FunctionZero.Maui.MvvmZero, 2.0.0-pre1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install FunctionZero.Maui.MvvmZero as a Cake Addin
#addin nuget:?package=FunctionZero.Maui.MvvmZero&version=2.0.0-pre1&prerelease

// Install FunctionZero.Maui.MvvmZero as a Cake Tool
#tool nuget:?package=FunctionZero.Maui.MvvmZero&version=2.0.0-pre1&prerelease

FunctionZero.Maui.MvvmZero

Major Improvements

This is a pre-release. All functionality for building and navigating pages is complete. Some interfaces may not be final.

Nuget, Source.

This is an evolution of MvvmZero for Xamarin

Overview

This library provides an exceptionally lightweight and easy to use framework for building cross-platform MAUI apps using the MVVM design pattern.

  • No naming conventions are enforced, so you can use your own, or none at all.
    • There is a recommended naming convention and folder structure for the less maverick amongst us.
  • The mapping of ViewModels to Views does not have to be 1:1
    • can be context-sensitive, or overridden at any time.
  • Navigation by ViewModel is recommended and has first-class support.
  • Page navigation is also supported, as is mix and match MAUI navigation and MvvmZero page or ViewModel navigation without things breaking!
  • Special support for Flyout and any derivatives.
  • Special support for IMultiPage<Page> (e.g. TabbedPage) and any derivatives.
  • ViewModel initialisation is typesafe, optional, and asynchronous if you want it to be.

Ethos

MvvmZero is there to guide the way, not get in the way.
If you understand the MVVM pattern and the UI stack for MAUI, the aim is for MvvmZero to be intuitive, and to remain so if you go off the beaten track to do things your own way.

QuickStart:

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>()

    // TestPage/Vm are transient because there can be more than one on any navigation stack at any time.
    .AddTransient<TestPage>()
    .AddTransient<TestPageVm>()
    ;

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;
    }
}

Samples

There are 3 sample applications in the repo. They are what I use for development, so they're not pretty.

  1. SampleApp. Has a root navigation page and pages can be pushed / popped.
  2. SampleFlyoutApp. This has a flyout managing multiple navigation pages, one of which is a TabbedPage.
  3. SampleTabbedApp. This has a TabbedPage managing 3 Tabs, 2 of which are navigation pages.

For each sample application, look at:

  1. MauiProgram.cs for registrations.
  2. App.xaml.cs to get things off the ground.

Workarounds

Currently the samples use AdaptedTabbedPage because TabbedPageBug and AdaptedFlyoutPage because AdaptedPageBug
If you target WinUI, you'll want to make any pushed pages Transient until this bug is fixed.

The rest is basically the same as found in the Xamarin Turorial

Is anybody out there?

Full documentation and better samples are on the way! Give me encouragement by starring the repo!
Whilst you're here, take a look at Maui.zBind and tell all your friends. It's already included in MvvmZero.

Product Compatible and additional computed target framework versions.
.NET net7.0-android33.0 is compatible.  net7.0-ios16.1 is compatible.  net7.0-maccatalyst16.1 is compatible.  net7.0-windows10.0.19041 is compatible.  net8.0-android was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.3 107 3/31/2024
8.0.2 79 3/31/2024
8.0.1 184 1/7/2024
8.0.0 167 10/21/2023
8.0.0-pre1 64 10/14/2023
2.0.4.2-pre1 115 8/26/2023
2.0.4.1 169 8/20/2023
2.0.4 132 8/19/2023
2.0.3 205 7/8/2023
2.0.2 133 6/27/2023
2.0.1 129 6/26/2023
2.0.0 154 4/23/2023
2.0.0-pre1 107 4/22/2023
1.1.2 210 3/29/2023
1.1.1 209 3/21/2023
1.1.0 212 3/18/2023
1.0.2 202 3/16/2023
1.0.1 196 3/15/2023
1.0.0 218 3/15/2023

This is a preview. All functionality for building and navigating pages is present. Interfaces may not be final. (any changes will be a simple refactor)