2015-10-21 69 views
0

我使用棱镜6.我有一个自定义RegionAdapter为(AvalonDock)LayoutDocumentPane。我使用的是这样的:棱镜区域从自定义RegionAdapter不显示在RegionManager列表

<!-- relevant lines from Shell.xaml. These regions are autoWired --> 
<ad:LayoutDocumentPaneGroup> 
    <ad:LayoutDocumentPane prism:RegionManager.RegionName="{x:Static inf:RegionNames.ContentRegion}"> 
    </ad:LayoutDocumentPane> 
</ad:LayoutDocumentPaneGroup> 
... 
<ContentControl prism:RegionManager.RegionName={x:Static inf:RegionNames.TestRegion}"> 
... 

我RegionAdapter:

public class AvalonDockLayoutDocumentRegionAdapter : RegionAdapterBase<LayoutDocumentPane> 
{ 
    public AvalonDockLayoutDocumentRegionAdapter(IRegionBehaviorFactory factory) : base(factory) 
    { 
    } 

    protected override void Adapt(IRegion region, LayoutDocumentPane regionTarget) 
    { 
     region.Views.CollectionChanged += (sender, e) => 
     { 
      OnRegionViewsCollectionChanged(sender, e, region, regionTarget); 
     }; 
    } 

    private void OnRegionViewsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e, IRegion region, LayoutDocumentPane regionTarget) 
    { 
     if (e.Action == NotifyCollectionChangedAction.Add) 
     { 
      foreach (var item in e.NewItems) 
      { 
       var view = item as FrameworkElement; 
       if (view != null) 
       { 
        var layoutDocument = new LayoutDocument(); 
        layoutDocument.Content = view; 

        var vm = view.DataContext as ILayoutPaneAware; 
        if (vm != null) 
        { 
         //todo bind to vm.Title instead 
         layoutDocument.Title = vm.Title; 
        } 

        regionTarget.Children.Add(layoutDocument); 
        layoutDocument.IsActive = true; 
       } 
      } 
     } else if (e.Action == NotifyCollectionChangedAction.Remove) 
     { 
      foreach (var item in e.OldItems) 
      { 
       var frameworkElement = item as FrameworkElement; 
       var childToRemove = frameworkElement.Parent as ILayoutElement; 

       regionTarget.RemoveChild(childToRemove); 
      } 
     } 
    } 

    protected override IRegion CreateRegion() 
    { 
     return new Region(); 
    } 
} 

当然,我与引导程序

... 
    protected override RegionAdapterMappings ConfigureRegionAdapterMappings() 
    { 
     var mappings = base.ConfigureRegionAdapterMappings(); 

     mappings.RegisterMapping(typeof(LayoutDocumentPane), Container.Resolve<AvalonDockLayoutDocumentRegionAdapter>()); 

     return mappings; 
    } 

    protected override void InitializeShell() 
    { 
     var regionManager = RegionManager.GetRegionManager(Shell); 
     // Here, regionManager.Regions only contains 1 Region - "TestRegion". 
     // Where is my region from the custom RegionAdapter? 

     Application.Current.MainWindow.Show(); 
    } 

    protected override DependencyObject CreateShell() 
    { 
     return Container.Resolve<Shell>(); 
    } 

    protected override void ConfigureModuleCatalog() 
    { 
     ModuleCatalog moduleCatalog = (ModuleCatalog)ModuleCatalog; 
     moduleCatalog.AddModule(typeof(HelloWorldModule.HelloWorldModule)); 
    } 
    ... 

而我的模块进行注册

... 
    public HelloWorldModule(IRegionManager regionManager, IUnityContainer container) 
    { 
     _regionManager = regionManager; 
     _container = container; 
    } 

    public void Initialize() 
    { 
     _container.RegisterTypeForNavigation<HelloWorldView>("HelloWorldView"); 

     // When uncommented, this next line works even though the region 
     // with this name doesn't appear in the list of regions in _regionManager 
     //_regionManager.RegisterViewWithRegion(RegionNames.ContentRegion, typeof(HelloWorldView)); 
    } 
... 

现在,请参阅Bootstrapper.InitializeShell呼叫和HelloWorldModule.Initialize呼叫,我只在RegionManager - “TestRegion”中有1个区域。如果我对我的“ContentRegion”输入registerViewWithRegion,它会将该视图的实例放入该区域,即使它未在区域中列出。

如果我试图从一个ICommand功能在我ShellViewModel导航(点击一个按钮为例),我可以导航到TestRegion但不是ContentRegion东西。看来我无法导航到由我的自定义RegionAdapter创建的任何区域。我错过了什么?

回答

0

很可能你的代码在下面导致你的问题。

protected override IRegion CreateRegion() { return new Region(); }

尝试改变返回可同时接待多个活动视图

protected override IRegion CreateRegion() 
{ 
    return new AllActiveRegion(); 
} 
0

我看到这个在your issue在GitHub上的区域。

取决于如何创建控件,您可能必须通过类似设置棱镜RegionManager在控制自己:

private readonly IRegionManager _regionManager; 

public AvalonDockLayoutDocumentRegionAdapter( 
    IRegionBehaviorFactory regionBehaviorFactory, 
    IRegionManager regionManager 
    ) : base(regionBehaviorFactory) { 
    this._regionManager = regionManager; 
} 

protected override void Adapt(IRegion region, LayoutDocumentPane target) { 
    RegionManager.SetRegionManager(target, this._regionManager); 
    // continue with Adapt 
} 
+0

我有一个类似的问题,但主要的问题是适应方法永远不会被调用,所以它没有任何区别。 –