How to register regions inside user controls or co

2019-03-06 01:14发布

问题:

I am using IRegionManager to load and navigate to views, I have no problem loading content to my main region in my main view which is loaded with my bootstrapper class but I cant load content to regions inside my loaded views, the region manager does not seem to be registering these regions.

my bootstrapper class:

protected override DependencyObject CreateShell()
{
    return this.Container.Resolve<MainWindowView>();
}

protected override void InitializeShell()
{
    Application.Current.MainWindow.Show();
}

protected override void ConfigureContainer()
{
    base.ConfigureContainer();

    this.Container.RegisterTypeForNavigation<DocumentView>();
    this.Container.RegisterTypeForNavigation<EmailView>();
    this.Container.RegisterTypeForNavigation<WorkTypeSelectionView>();
}

the DocumentView is user control with another region the method that runs when the command is triggered is this:

private void ViewEmailAction()
{
    NavigationParameters parameters;
    parameters = new NavigationParameters();
    parameters.Add(nameof(this.CurrentEmail), this.CurrentEmail);
    this.regionManager.Regions[this.EmailRegion].RequestNavigate(nameof(EmailView), parameters);
}

This throws and exception with the message "The region manager does not contain the EmailRegion region."

Thanks in advance!

回答1:

There are two different things going on here:

UserControls: This should work with not issues whatsoever. Chances are that you are trying to navigate to a region that is defined in a View that hasn't been loaded yet. Make sure you are navigating to a region after it has been loaded. Navigating inside of ViewModel constructors is one of the biggest sources of this problem. If you want to post your sample to GitHub, I can take a look.

ControlTemplates: This is a known issue in Prism. Here is your fix:

http://southworks.com/blog/2011/11/10/regions-inside-datatemplates-in-prism-v4-using-a-region-behavior/



标签: c# .net wpf Prism