Language

IModalNavigationPlatformFactory Interface

Definition

Application-wide factory for creating IModalNavigationPlatform instances. Register an implementation in the application's IServiceCollection to replace the built-in modal presentation for each window the factory chooses to handle.

public interface class IModalNavigationPlatformFactory
public interface IModalNavigationPlatformFactory
type IModalNavigationPlatformFactory = interface
Public Interface IModalNavigationPlatformFactory

Remarks

When an IModalNavigationPlatformFactory is present in Services for a window, the framework calls CreateModalNavigationPlatform(IModalNavigationHost) once for that window and routes every modal push and pop to the returned instance instead of the built-in platform code. This lets alternative platform backends render modal navigation without reflection, partial classes or forking, while the framework keeps ownership of the cross-platform modal stack, the page lifecycle events and the push/pop reconciliation loop.

The factory is resolved from the window's service scope, so it may be registered with any lifetime. The framework calls it once per window and disposes the returned instance, so return a new IModalNavigationPlatform for each call. It is called again for the same window only if the window gets a new handler, which brings a new service scope (for example an Android activity recreation).

If this method throws, the framework logs the exception and permanently falls back to the built-in platform for that window rather than rethrowing from whatever call site happened to trigger resolution. Creation is not retried, so a failed registration fails deterministically and visibly in the log instead of throwing repeatedly from arbitrary navigation code. Prefer returning null over throwing when a backend deliberately does not want to handle a window.

Registering a backend that presents modals with its own native window stack:
public sealed class MyModalNavigationPlatformFactory : IModalNavigationPlatformFactory
{
    public IModalNavigationPlatform? CreateModalNavigationPlatform(IModalNavigationHost host)
        => new MyModalNavigationPlatform(host);
}

public sealed class MyModalNavigationPlatform : IModalNavigationPlatform
{
    readonly IModalNavigationHost _host;
    readonly MyNativeModalStack _stack;

    public MyModalNavigationPlatform(IModalNavigationHost host)
    {
        _host = host;
        // Captured while the instance is live: Dispose must not depend on the MauiContext.
        _stack = MyNativeModalStack.For(host.MauiContext);
    }

    public bool IsReady => _host.IsWindowReady && _stack.IsRealized;

    public async Task PushModalAsync(Page modal, bool animated)
    {
        var nativeView = modal.ToPlatform(_host.MauiContext);
        await _stack.PushAsync(nativeView, animated && !_host.IsBatchPushing);
    }

    public async Task PopModalAsync(Page modal, bool animated)
    {
        // Idempotent: the modal may already be gone when the user dismissed it natively.
        if (_stack.Contains(modal))
            await _stack.PopAsync(animated && !_host.IsBatchPopping);

        modal.DisconnectHandlers();
    }

    public void PageAttached() =>
        _stack.BackButtonPressed = () => _host.CurrentPage?.SendBackButtonPressed() ?? false;

    // A native dismissal the framework can't see has to be routed back through Navigation so the
    // cross-platform stack and the page lifecycle events stay in sync.
    void OnDismissedNatively() => _host.Window.Navigation.PopModalAsync(animated: false);

    public void Dispose()
    {
        // Still-presented modals are NOT popped by the framework during teardown.
        _stack.DismissAll();
    }
}

// In MauiProgram:
builder.Services.AddSingleton<IModalNavigationPlatformFactory, MyModalNavigationPlatformFactory>();

Methods

Name Description
CreateModalNavigationPlatform(IModalNavigationHost)

Creates the IModalNavigationPlatform for a window.

Applies to