首页 > 解决方案 > Why is there no prism region registred in my regionManager?

问题描述

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;
    }
}

标签: c#prism

解决方案


手动设置区域(从构造函数或其他地方)是要走的路。

但是您不能只ContentControl从其自动创建的字段中引用它,您必须在模板中查找它GetTemplateChild及其部件名称。

我怀疑这对于shell (实际上是任何区域托管控件)可能有点问题,因为按照惯例,您的控件预计可以处理缺失的部分。你至少应该对那些创建区域部分是强制性的主题的人做一个说明。另外,添加这些TemplatePart属性...


推荐阅读