为什么我的regionManager中没有注册棱镜区域?

时间:2019-01-12 21:54:47

标签: c# prism

我正在尝试设置我的第一个棱镜应用程序。我有一个简单的MainWindowShell,它的contentControl带有一个棱镜regionName和一个在该区域中显示的ViewA。现在我想从ViewA切换到ViewB,但是导航出现问题。一切似乎都连接良好,但是当涉及regionManager.RequestNavigate(...)时,regionManager中没有注册任何区域,导航失败。我想我已经找到了问题所在,但是我无法解决。

我正在使用customControl。因此没有xaml。 xaml来自resourceDictionary中的controlTemplate /样式,这似乎是问题所在。

我已经尝试过使用userControl进行同样的操作,从controlTemplate复制代码,瞧!我的区域已在regionManager中完美注册。但是我不想使用userControls,因为每个viewElement是通过resourceDictionary通过样式和模板自定义的。因此,我会有很多空的无用的“ xyUserControl.xaml”

我以为我可以在stackoverflow(Prism Regions from Custom RegionAdapter Not Showing in RegionManager List)上找到我的问题的解决方案,或者至少是一种解决方法。但是我不知道该怎么办,或者甚至可能。因为解决方案是在后面的代码中设置regionManager,如下所示: RegionManager.SetRegionManager(targetContentControl,regionManager);但是我后面的代码对xaml-template一无所知。所以我拼命地尝试了这个:RegionManager.SetRegionManager(this,regionManager); ...没有工作。我没主意了。

这是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>

和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 个答案:

答案 0 :(得分:0)

(通过构造函数或其他方式)手动设置区域是一种方法。

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

我怀疑对于 shell (实际上是任何托管区域的控件)来说,可能会有些问题,因为按照惯例,您的控件应该可以使用缺少的部分。您至少应注意那些创建主题的区域部分是必填的主题。另外,添加这些TemplatePart属性...

相关问题