2015-10-21 54 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

我有一個類似的問題,但主要的問題是適應方法永遠不會被調用,所以它沒有任何區別。 –