Goldie.MauiPlugins.PageResolver 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Goldie.MauiPlugins.PageResolver --version 1.0.0
NuGet\Install-Package Goldie.MauiPlugins.PageResolver -Version 1.0.0
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="Goldie.MauiPlugins.PageResolver" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Goldie.MauiPlugins.PageResolver --version 1.0.0
#r "nuget: Goldie.MauiPlugins.PageResolver, 1.0.0"
#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 Goldie.MauiPlugins.PageResolver as a Cake Addin
#addin nuget:?package=Goldie.MauiPlugins.PageResolver&version=1.0.0

// Install Goldie.MauiPlugins.PageResolver as a Cake Tool
#tool nuget:?package=Goldie.MauiPlugins.PageResolver&version=1.0.0

MAUI PageResolver

A simple and lightweight page resolver for use in .NET MAUI projects.

While we're waiting for the full MVVM frameworks we know and love to be updated for MAUI, or if you just want a simple page resolver with DI without using a full MVVM framework (or if you want to use MVU), this package will let you navigate to fully resolved pages, with view models and dependencies, by calling:

await Navigation.PushAsync<MyPage>();

Getting Started

Step 1: Install Nuget package

Install the Nuget package

dotnet add package Goldie.MauiPlugins.PageResolver

Step 2: Register dependencies

Your services, view models, and pages all need to be registered in the service collection. Update the Configure method in your Startup.cs as follows

using Microsoft.Maui;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Controls.Compatibility;
// Add reference to PageResolver
using Maui.Plugins.PageResolver;
// Add any required references to your services, view models, etc.
using MyApp.Services;
using MyApp.ViewModels;
using MyApp.Views;

namespace MyApp
{
    public class Startup : IStartup
    {
        public void Configure(IAppHostBuilder appBuilder)
        {
            appBuilder
                .UseFormsCompatibility()
                .UseMauiApp<App>()
                // Add ConfigureServices
                .ConfigureServices(services =>
                {
                    // register your services
                    services.AddSingleton<IMyService, MyService>();

                    // register your view models
                    services.AddTransient<MyViewModel>();

                    // register your views
                    services.AddTransient<MyPage>();

                    // register the page resolver
                    services.UsePageResolver();
                })
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                });
        }
    }
}

Step 3: Inject your dependencies

Use constructor injection to add your dependencies to your pages and view models. E.g.:

public class MyPage
{
    public MyViewModel ViewModel { get; set; }

    public MyPage(MyViewModel viewModel)
    {
        ViewModel = viewModel;
        BindingContext = ViewModel;
    }
}

And in your view models:

public class MyViewModel
{
    public IMyService Service { get; set; }

    public MyViewModel(IMyService myService)
    {
        Service = myService;
    }
}

Step 4: Use in your code

In your code, navigate to your page by calling:

await Navigation.PushAsync<MyPage>();

This will return a fully resolved instance of MyPage including all dependencies.

Modal pages are also supported:

await Navigation.PushModalAsync<MyPage>();

Notes

This is just something I put together for myself (with some input from William Liebenberg), but thought it might be useful to others. If you use it and it's helpful, great. If not, please remember it's an early attempt at doing something useful for a preview version of MAUI. If you have comments or suggestions, feedback is welcome.

TODO

  • Use reflection / code generation to automatically register pages and view models
  • (Pending C# 10) Add a global using for this package
  • Set up GitHub Action to publish package
  • (Possibly) change namespace
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos 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
2.0.1 1,796 11/18/2023
2.0.0 141 11/13/2023
2.0.0-preview1 72 10/13/2023
1.1.1 3,416 1/14/2023
1.1.0 254 1/14/2023
1.0.7 940 8/13/2022
1.0.6 448 6/26/2022
1.0.5 338 3/8/2022
1.0.5-preview1 109 3/5/2022
1.0.4 132 2/26/2022
1.0.3 112 2/26/2022
1.0.2 138 2/14/2022
1.0.1 234 10/23/2021
1.0.0 225 6/17/2021
1.0.0-build 125 8/13/2022