1

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

Rob Goodwin
  • 2,676
  • 2
  • 27
  • 47

1 Answers1

4

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);
}
  • I will dig into figuring out if they are realized. Using the VS2017 tools to show the live visual tree, the ContentControls show up in the list, but the regions do not appear to be contained in the region manager. The navigation request happens as a result of a user clicking a list box item, so it is done after the startup/initialization of the application has happened. – Rob Goodwin Jun 15 '17 at 23:02
  • The contentcontrols probably aren't added to the visual tree until the navigation panes are expanded. Just a guess. –  Jun 15 '17 at 23:20
  • The content controls are open when I issue the command to navigate. I will try to pare down what I have into a standalone example. – Rob Goodwin Jun 16 '17 at 01:42
  • I put together a standalone sample located here: https://github.com/robgoodwin/samples.git. I am sure it must be how I am handling the custom control as you suggested, but have not found the solution yet. Thanks for your time. – Rob Goodwin Jun 16 '17 at 14:36
  • Thanks for the sample. Check out my updated response. It works now :) –  Jun 16 '17 at 16:52