c# - 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;
}
}
解决方案
手动设置区域(从构造函数或其他地方)是要走的路。
但是您不能只ContentControl
从其自动创建的字段中引用它,您必须在模板中查找它GetTemplateChild
及其部件名称。
我怀疑这对于shell (实际上是任何区域托管控件)可能有点问题,因为按照惯例,您的控件预计可以处理缺失的部分。你至少应该对那些创建区域部分是强制性的主题的人做一个说明。另外,添加这些TemplatePart
属性...
推荐阅读
- javascript - 无法导出多个云功能
- maven - Sonatype Nexus 出于某种未知原因给出 401
- c# - 为什么应用程序不使用我的连接字符串?
- c - 如何在 C 语言中集成语音到字符串检测器
- javascript - 为什么在像减法这样的简单计算之后我会得到像 (5.0306980803327406e-17) 这样的数字?
- python - 转换 struct_time 最后给我“.%fZ”而不是时区和微秒
- mysql - 计数和求和条件大于某事
- python-3.x - BeautifulSoup4 - 在两个不同的标签之间连接多个 html 元素
- linux - 在 NGINX 上通过 HTTP/HTTPS 拒绝所有非 443 或 80 端口的连接
- html - 如何让我的 flexbox 页脚占据底部的整个空间?