0
votes

I am trying to setup my first prism application. I have a simple MainWindowShell with a contentControl with a prism regionName and a ViewA displayed in this region. Now i want to switch from ViewA to ViewB, but i have problems with the navigation. Everything seems to be perfectly wired up, but when it comes to regionManager.RequestNavigate(...) there is no region registred in the regionManager and the navigation fails. I think i have tracked the problem, but i can not solve it.

I am using a customControl. So there is no xaml. The xaml comes from a controlTemplate/styling in a resourceDictionary and this seems to be the problem.

I have tried the same with a userControl, copied the code from the controlTemplate and voila! there was my region perfectly registred in the regionManager. But i don't want to use userControls, because every viewElement is customized from a resourceDictionary by stylings and templates. So i would have a lot of empty useless "xyUserControl.xaml"s

I thought i could have found a solution - or at least a workaround - for my problem here on stackoverflow (Prism Regions from Custom RegionAdapter Not Showing in RegionManager List). But i don't know how to do that in my case or if it's even possible. Because the solution would be to set the regionManager in the code behind like this: RegionManager.SetRegionManager(targetContentControl, regionManager); But my code behind doesn't know anything from the xaml-template. So i desperately tried this: RegionManager.SetRegionManager(this, regionManager); ...didn't work. I am out of ideas.

Here is the RessourceDictionary:

  <Style TargetType="{x:Type userControls:MainWindowShell}">
    <Setter Property="Template" Value="{DynamicResource MainWindowShell}" />
  </Style>

  <ControlTemplate x:Key="MainWindowShell" TargetType="{x:Type userControls:MainWindowShell}">
      <Grid>
      <ContentControl regions:RegionManager.RegionName="{x:Static utilities:RegionNames.Content}">
      </ContentControl>
      </Grid>
  </ControlTemplate>

And the MainWindowShell:

public class MainWindowShell : Window
{
    static MainWindowShell()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MainWindowShell), new FrameworkPropertyMetadata(typeof(MainWindowShell)));
    }

    public MainWindowShell(IMainMenuViewModel viewModel, IRegionManager regionManager)
    {
        this.DataContext = viewModel;
    }
}
1

1 Answers

0
votes

Setting the region manually (from the constructor or elsewhere) is the way to go.

But you cannot just reference the ContentControl from its automagically created field, you have to look it up in the template with GetTemplateChild and its part-name.

I suspect that it may be a bit problematic for the shell (in fact any region-hosting control) as your control is expected to work with missing parts by convention. You should at least make a note to those creating the themes that the region-parts are mandatory. Also, add those TemplatePart attributes...