【发布时间】:2018-11-03 16:18:50
【问题描述】:
我有一个使用 Prism 的基于视图的导航的复杂场景。我正在尝试做的是在父模块的导航区域中为某些模块定义一个新的 NavigationRegion。
我会解释自己:
我的解决方案中有以下项目:
- 壳牌
- Shell.Module1
- Shell.Module2
- Shell.Module3
- Shell.Module3.SubModule1
- Shell.Module3.SubModule2
在 shell 视图中,我定义了 MainNavigationRegion 和 MainContentRegion。 模块 1 和模块 2,将导航项加载到 MainNavigationRegion,将视图加载到 MainContentRegion。这工作正常。
Module3 带来了复杂性,因为 Module 3 本身没有任何功能。这是加载到 MainNavigationRegion 中的“Shell.Module3”项目的 NavigationItemView:
<Grid HorizontalAlignment="Center">
<materialDesign:PopupBox x:Name="NavigateToToolsRadioButton"
AutomationProperties.AutomationId="ToolsRadioButton" PopupMode="Click"
StaysOpen="False" UseLayoutRounding="False"
Style="{StaticResource MaterialDesignMultiFloatingActionAccentPopupBox}"
PlacementMode="RightAndAlignMiddles">
<StackPanel Orientation="Horizontal" x:Name="NavigationItemsControl"
prism:RegionManager.RegionName="ToolsNavigationRegion">
</StackPanel>
</materialDesign:PopupBox>
</Grid>
在 Module3 的 NavigationItemView 中(它加载在 MainNavigationRegion 中),我正在为 Module 3 的子模块定义一个新的 NavigationRegion。 但是,在 Module3.SubModule1 类的 Initialize() 方法中,我收到此错误:“区域管理器不包含 ToolsNavigationRegion 区域。” 方法是这样的:
public void Initialize()
{
var navitagionView = Container.Resolve<EarnedValueMethodNavigationItemView>();
RegionManager.Regions[RegionNames.ToolsNavigationRegion].Add(navitagionView);
var mainView = Container.Resolve<EarnedValueMethodView>();
RegionManager.Regions[RegionNames.MainContentRegion].Add(mainView);
}
如果我调试 RegionManager 属性,我会看到 ToolsNavigationRegion 不在其中。
如果我改变这一行:
RegionManager.Regions[RegionNames.ToolsNavigationRegion].Add(navitagionView);
通过另一行:
RegionManager.Regions[RegionNames.MainNavigationRegion].Add(navitagionView);
然后,它工作正常,但显然导航项放置在主导航区域中,我希望将它放在父模块的导航区域项下。我想要完成的事情有可能吗?
编辑:
我还创建了 StackPanel RegionAdapter,如下所示:
public class StackPanelRegionAdapter : RegionAdapterBase<StackPanel>
{
public StackPanelRegionAdapter(IRegionBehaviorFactory factory)
: base(factory)
{
}
protected override void Adapt(IRegion region, StackPanel regionTarget)
{
region.Views.CollectionChanged += (s, e) =>
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (FrameworkElement element in e.NewItems)
{
regionTarget.Children.Add(element);
}
}
//implement remove
};
}
protected override IRegion CreateRegion()
{
return new AllActiveRegion();
}
}
【问题讨论】:
-
弹出框的内容是否在可视化树中?如果没有,区域管理器将找不到区域名称属性。在这种情况下,您必须从构造函数中手动添加区域,例如 stackoverflow.com/questions/41212881/…
-
谢谢@Haukinger。你完全正确。
-
没问题,只是从评论中做出回答,以提高未来访客的知名度......
标签: mvvm navigation prism region