Skip to content
This repository was archived by the owner on Nov 8, 2025. It is now read-only.

1 Using the PageResolver

Matt Goldman edited this page Mar 5, 2022 · 15 revisions

1.1 Install Nuget package

Install the Nuget package

dotnet add package Goldie.MauiPlugins.PageResolver

1.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 static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
            .UseMauiApp<App>()				
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
            });

            // register your services
            builder.Services.AddSingleton<IMyService, MyService>();

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

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

            // register the page resolver
            builder.Services.UsePageResolver();

            return builder.Build();
        }
    }
}

You can also register the PageResolver using the fluent API:

MauiAppBuilder? builder = MauiApp.CreateBuilder();
builder
    .UseMauiApp<App>()
    .UseMauiCommunityToolkit()
    .UsePageResolver()
    .ConfigureFonts(fonts =>
    {
        fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
        fonts.AddFont("la-solid-900.ttf", "LASolid");
    });

return builder.Build();

(Thanks to @IeuanWalker). Just like the first approach, you need to make sure to register your dependencies before registering the PageResolver.

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

1.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>();

It might be helpful to add a global using for the PageResolver so that you don't have to reference it in every file. If you have an _Imports.cs file in your project (or other file somewhere that you register all your global usings), add the reference in there:

global using Maui.Plugins.PageResolver;

If you don't have a file for registering all your global usings, you can add this anywhere in your project. But a single global usings file is a good idea.

1.5 Use Parameters

You can pass parameters required by your Page's constructor. For example, consider the following constructor:

public MyPage(MyViewModel viewModel, string userName, int productId)
{
    ...
}

You can use PageResolver to pass these parameters when pushing a MyPage onto the navigation stack:

await Navigation.PushAsync<MyPage>("bob", 4);

Note that the MyViewModel doesn't need to be passed as a parameter. Anything registered with the service provider will be resolved automatically.

WARNING: Note that parameters are not type checked, so passing an incorrect set of parameters can lead to runtime errors.

Also note that when using PageResolver with parameters, reflection is used to match the parameters to a constructor with a matching signature. So Pages with parameters will take a few nanoseconds more than without.

Clone this wiki locally