Jinjinov
Jinjinov

Reputation: 2683

Prism RequestNavigate from PrismApplication immediately on start

In Prism 7 I can RegisterForNavigation and RequestNavigate from IModule like this:

public class ModuleAModule : IModule
{
    public void OnInitialized(IContainerProvider containerProvider)
    {
        var regionManager = containerProvider.Resolve<IRegionManager>();
        regionManager.RequestNavigate("ContentRegion", "PersonList");
    }

    public void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterForNavigation<PersonList>();
    }
}

and I know that I can RegisterForNavigation from PrismApplication like this:

public partial class App : PrismApplication
{
    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterForNavigation<ViewA>();
    }
}

but how can I RequestNavigate from PrismApplication immediately on start?

I have tried this:

public class MainWindowViewModel : BindableBase
{
    public MainWindowViewModel(IRegionManager regionManager)
    {
        regionManager.RequestNavigate("ContentRegion", "ViewA");
    }
}

but this.regions.Count is 0 in RegionManager from Prism

private IRegion GetRegionByName(string regionName)
{
    return this.regions.FirstOrDefault(r => r.Name == regionName);
}

"ContentRegion" definitely exists, because it works if I try from the IModule and I know that RegisterTypes from PrismApplication executes before the MainWindowViewModel constructor.

I don't know what I am missing and I can't find the answer in any examples or tutorials.

Thank you for your help!

Upvotes: 1

Views: 742

Answers (1)

Haukinger
Haukinger

Reputation: 10873

Your best bet is to override OnInitialized in your application and do the navigation there. You can access the container to fetch the region manager through the Container property.

If you use a bootstrapper, you can override InitializeModules and navigate there.

Upvotes: 4

Related Questions