Caliburn-Micro / Caliburn.Micro

A small, yet powerful framework, designed for building applications across all XAML platforms. Its strong support for MV* patterns will enable you to build your solution quickly, without the need to sacrifice code quality or testability.
http://caliburnmicro.com/
MIT License
2.79k stars 775 forks source link

MAUI Navigation #897

Open ClaytonIndustries opened 5 months ago

ClaytonIndustries commented 5 months ago

Hi I'm having trouble working out how to use the INavigationService in MAUI. In Xamarin Forms we used to override PrepareViewFirst to inject the NavigationPageAdapter object into the container. In MAUI though PrepareViewFirst is never called when using DisplayRootViewForAsync.

Here's what I've got

public App()
{
    InitializeComponent();

    Initialize();

    _container = container;

    _container.PerRequest<HomeViewModel>();
    _container.PerRequest<AnotherViewModel>();

    DisplayRootViewForAsync<HomeViewModel>();
}

// This is never called so no NavigationService will get added to the container
protected override void PrepareViewFirst(NavigationPage navigationPage)
{
    _container.Instance<INavigationService>(new NavigationPageAdapter(navigationPage));
}

From looking at the source code I tried calling PrepareViewFirst() before DisplayRootViewForAsync and that successfully added a NavigationPageAdapter to the container however when INavigationService is injected into a viewmodel calling NavigateToViewModelAsync does nothing.

So how do we navigate between viewmodels in MAUI?

I'm using the latest beta version from MyGet