是否有可能通过regionManager.RequestNavigate将其名称注册为View以便将其从区域中删除?
问题描述:
在我的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不知道有关模块的意见。有没有办法删除视图,当它没有名称?感谢任何提醒
答
谢谢卢克。我发现我的问题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);
}
答
的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));
如果这还不够,你仍然可以将视图对象添加额外的属性,并试图找回他们铸造的意见到合适的类型。
谢谢为了你的帮助,卢克。我发布了我的最终解决方案。 – Max