基于视图的导航棱镜的复杂场景-区域管理器不包含''区域 [英] Complex scenario with View-Based Navigation Prism - The region manager does not contain the ' ' region

查看:290
本文介绍了基于视图的导航棱镜的复杂场景-区域管理器不包含''区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Prism的基于视图的导航的复杂场景.我想做的是在父模块的navitagion区域中为某些模块定义一个新的NavigationRegion.

I have a complex scenario with View-Based Navigation using Prism. What I am trying to do, is defining a new NavigationRegion for some modules, in the navitagion region of the parent module.

我会自我解释:

我的解决方案中包含以下项目:

I have the following projects in my solution:

  • 壳牌
  • Shell.Module1
  • Shell.Module2
  • Shell.Module3
  • Shell.Module3.SubModule1
  • Shell.Module3.SubModule2

在外壳视图中,我定义了MainNavigationRegion和MainContentRegion. 模块1和2,将导航项加载到MainNavigationRegion中,将视图加载到MainContentRegion中.很好.

In the shell view, I define the MainNavigationRegion and the MainContentRegion. Module 1 and 2, load the navigation item into the MainNavigationRegion and the View into the MainContentRegion. That is working fine.

Module3带来了复杂性,因为Module 3本身没有任何功能.这是加载到MainNavigationRegion中的"Shell.Module3"项目的NavigationItemView:

The complexity comes with Module3, since Module 3 itself has no functionality. This is this NavigationItemView of the "Shell.Module3" project that is loaded into the MainNavigationRegion:

<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中),我正在为模块3的子模块专门定义一个新的NavigationRegion. 但是,在Module3.SubModule1类的Initialize()方法中,出现以下错误:区域管理器不包含ToolsNavigationRegion区域." 这是方法:

In the NavigationItemView of Module3 (that it's loaded in the MainNavigationRegion), I am defining a new NavigationRegion specifically for the submodules of Module 3. However, in the Initialize() method of the Module3.SubModule1 class, I get this error: 'The region manager does not contain the ToolsNavigationRegion region.' This is the method:

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不在其中.

If I debug RegionManager property, I see that ToolsNavigationRegion is not in there.

如果我更改此行:

RegionManager.Regions[RegionNames.ToolsNavigationRegion].Add(navitagionView);

另一行:

RegionManager.Regions[RegionNames.MainNavigationRegion].Add(navitagionView);

然后,它工作正常,但显然导航项位于主导航区域"中,我希望将其放在父级"模块的导航区域"项目下.我可能要完成什么?

then, it works fine, but obviously the navigation item is placed in the Main Navigation Region and I would like to have it under the Navigation Region item of the Parent module. Is it possible what I am trying to accomplish?

我还如下创建了StackPanel RegionAdapter:

I also created the StackPanel RegionAdapter as follows:

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

推荐答案

扩展上面的评论:

区域管理器将通过搜索可视树找到标记有区域名称属性的区域.如果在区域管理器进行搜索时某个区域不在视觉树中,则不会创建任何区域.例如,按需创建的弹出窗口就是这种情况.

The region manager will find regions marked with the region name attribute by searching the visual tree. If a region is not in the visual tree when the region manager does its search, no region will be created. This is the case for popup windows, for example, that are created on demand.

在这种情况下,必须在构造弹出窗口期间手动分配区域.

In that case, the region must be manually assigned during construction of the popup window.

复制答案:

可能您需要在(构造函数)后面的弹出视图代码中手动设置区域管理器,如下所示:

Probably you need to set the region manager manually, in the popup view's code behind (constructor), like this:

RegionManager.SetRegionName( theNameOfTheContentControlInsideThePopup, WellKnownRegionNames.DataFeedRegion );
RegionManager.SetRegionManager( theNameOfTheContentControlInsideThePopup, theRegionManagerInstanceFromUnity );

您必须为托管该区域的内容控件分配一个名称,并以某种方式获取区域管理器(ServiceLocator.Current.GetInstance<IRegionManager>()).

You'll have to assign a name to the content control hosting the region and somehow acquire the region manager (ServiceLocator.Current.GetInstance<IRegionManager>()).

这篇关于基于视图的导航棱镜的复杂场景-区域管理器不包含''区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆