C# 是否可以使用它注册视图';是否通过regionManager.RequestNavigate将其从区域中删除?
在my PRISM应用程序中,用户可以在选项卡视图中打开模块视图(C# 是否可以使用它注册视图';是否通过regionManager.RequestNavigate将其从区域中删除?,c#,wpf,prism,C#,Wpf,Prism,在my PRISM应用程序中,用户可以在选项卡视图中打开模块视图(Navigate(“TestView”))。现在我想通过OnCloseTab(“TestView”)关闭此视图,但注册的视图没有名称 public class MainWindowViewModel: BindableBase { ... private void Navigate(string uri) { this.regionManager.RequestNavigate("TabRegion
Navigate(“TestView”)
)。现在我想通过OnCloseTab(“TestView”)
关闭此视图,但注册的视图没有名称
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));
}
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);
}
它与:
IRegion region = regionManager.Regions["TabRegion"];
object view = region.GetView("TestView");
if (view == null)
{
view = ServiceLocator.Current.GetInstance<Views.TestView>();
region.Add(view, "TestView");
}
IRegion region=regionManager.Regions[“TabRegion”];
对象视图=region.GetView(“TestView”);
如果(视图==null)
{
view=ServiceLocator.Current.GetInstance();
添加(视图,“TestView”);
}
但是MainWindowViewModel不知道有关模块视图的信息。当视图没有名称时,是否有方法删除该视图?感谢您的建议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));
如果这还不够,您仍然可以在视图对象上添加额外属性,并尝试检索它们,将视图转换为合适的类型。谢谢。我找到了解决问题的办法 在我的
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>
谢谢你的帮助,卢克。我已经发布了我的最终解决方案。