Search code examples
wpfprismprism-6

Prism 6 Region Manager RequestNavigate fails to navigate for some regions


I have a user control where I have multiple Prism regions defined for injecting views. I decided to use the Prism view navigation for dealing with switching my "SelectedMenuContentRegion" based on user actions (as shown below) and I have run into a problem. I am sure the problem is with my usage, but I have not been able to figure out what I am doing wrong. I have the following user control which contains a custom WPF control.

<Grid>
  <commonwpfcontrols:NavigationPane Background="{StaticResource SecondaryColorBrush}" IsExpanded="False" MenuItems="{Binding MenuItems}">
     <commonwpfcontrols:NavigationPane.Content>
        <ContentControl prism:RegionManager.RegionName="MapRegion"/>
     </commonwpfcontrols:NavigationPane.Content>
     <commonwpfcontrols:NavigationPane.SelectedMenuContent>
        <ContentControl prism:RegionManager.RegionName="SelectedMenuContentRegion"/>
     </commonwpfcontrols:NavigationPane.SelectedMenuContent>
  </commonwpfcontrols:NavigationPane>

  <Grid>
     <ContentControl prism:RegionManager.RegionName="ApplicationOverlay"/>
  </Grid>

There are 3 regions defined. If I do the standard

mRegionManager.RegisterViewWithRegion("SelectedMenuContentRegion", () => mUnityContainer.Resolve<MapSettingsView>());

It works as expected, however, if I register the view for navigation like the following:

mUnityContainer.RegisterTypeForNavigation<MapSettingsView>();

and then attempt to navigate sometime later

mRegionManager.RequestNavigate("SelectedMenuContentRegion ", "MapSettingsView", NavigationComplete);

It fails. I Noticed in the debugger that the region manager only had the “ApplicationOverlay” region in it's list of regions. So, I changed the region that I was navigating to, to the ApplicationOverlay region as a test, and it worked. I am getting the region manager through dependency injection. Any clue as to why the other defined regions are not known to the region manager?

Update Since more detailed information is required, I created a small standalone sample that shows the failed navigation. Prism Navigation Sample


Solution

  • This depends on the custom control you are using. It is possible that the navigation panes at not part of the visual tree (or initialized) until later. The reason the RegisterViewWithRegion would work is because it waits until the region have been realized before injecting. So this tell me that you are trying to navigate before the regions have been initialized.

    UPDATE: Thanks for the sample, it helps repo the problem. Honestly, I didn't spend any time trying to figure out why it wasn't working, but instead I just got it to work. All you have to do it give your regions an x:Name, and then set the region manager using the attached property in code:

    <ContentControl x:Name="_rightContents" />
    

    And then in code-behind:

    public MainWindow(IRegionManager regionManager)
    {
        InitializeComponent();
        RegionManager.SetRegionManager(_rightContents, regionManager);
    }