2017-10-18 75 views
1

在我的PRISM-App中,用戶可以在TabView中打開模塊的視圖(Navigate("TestView"))。現在我想通過OnCloseTab("TestView")關閉此視圖,但註冊視圖沒有名稱。是否有可能通過regionManager.RequestNavigate將其名稱註冊爲View以便將其從區域中刪除?

public class MainWindowViewModel: BindableBase 
{ 
... 
    private void Navigate(string uri) 
    { 
     this.regionManager.RequestNavigate("TabRegion", uri);    
    } 

    private void OnCloseTab(string uri) 
    { 
     IRegion region = this.regionManager.Regions["TabRegion"]; 

     object view = region.GetView(uri); 
     if (view != null) 
     { 
      region.Remove(view); 
     } 

    } 
} 

模塊註冊在我的引導程序是這樣的:

protected override void ConfigureModuleCatalog() 
{ 
    base.ConfigureModuleCatalog(); 

    ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog; 
    Type modulePType = typeof(Module.ProductionData.ProductionDataModule);       
    moduleCatalog.AddModule(typeof(Module.ProductionData.ProductionDataModule));    
} 

它適用於:

IRegion region = regionManager.Regions["TabRegion"]; 

object view = region.GetView("TestView"); 
if (view == null) 
{ 
    view = ServiceLocator.Current.GetInstance<Views.TestView>(); 
    region.Add(view, "TestView"); 
} 

但MainWindowViewModel不知道有關模塊的意見。有沒有辦法刪除視圖,當它沒有名稱?感謝任何提醒

回答

0

謝謝盧克。我發現我的問題here

的解決方案在我MainWindowView.cs(XAML)我已經添加了以下內容:

<TabControl.ItemTemplate> 
    <DataTemplate> 
     <DockPanel Width="Auto"> 
      <Button Command="{Binding DataContext.CloseTabCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
    CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}" 
    Content="X" 
    Cursor="Hand" 
    DockPanel.Dock="Right" 
    Focusable="False" 
    FontFamily="Courier" 
    FontWeight="Bold" 
    Margin="4,0,0,0" 
    FontSize="10" 
    VerticalContentAlignment="Center" 
    Width="15" Height="15" /> 

      <ContentPresenter Content="{Binding DataContext.DataContext.ViewTitle, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}" /> 
     </DockPanel> 
    </DataTemplate> 
</TabControl.ItemTemplate> 

在我MainWindowViewModel.cs我改變了我的CloseCommand這樣的:

public DelegateCommand<object> CloseTabCommand { get; set; } 

public MainWindowViewModel(IRegionManager regionManager) 
{ 
    this.regionManager = regionManager; 

    CloseTabCommand = new DelegateCommand<object>(OnCloseTab); 

} 
private void OnCloseTab(object tabItem) 
{ 
    var view = ((System.Windows.Controls.TabItem)tabItem).DataContext; 
    this.regionManager.Regions["TabRegion"].Remove(view); 
} 
1

RequestNavigate("TabRegion", uri)方法使用以下方法在內部將所選擇的視圖的區域:

IRegionManager Add(object view, string viewName, bool createRegionManagerScope) 

稱爲具有以下參數:

RegionManager.Add(view, null, false); 

所以沒有名關聯到導航視圖。因此,使用視圖名稱/ uri檢索視圖對象是不可能的。另一種方法是嘗試匹配視圖的.NET類型:

object view = region.Views.FirstOrDefault(v => v.GetType() == typeof(yourViewType)); 

如果這還不夠,你仍然可以將視圖對象添加額外的屬性,並試圖找回他們鑄造的意見到合適的類型。

+0

謝謝爲了你的幫助,盧克。我發佈了我的最終解決方案。 – Max

相關問題