区域已在导航上注册错误 [英] Regions already registered error on navigation

查看:53
本文介绍了区域已在导航上注册错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Shell.xaml 中有一个 TabControl 区域:

I have a TabControl region in Shell.xaml:

<TabControl Name="TabRegionControl" prism:RegionManager.RegionName="TabRegion" />

用户可以点击一个按钮来打开一个新的标签页,它调用:

The user can click a button to open a new tab, which calls:

RegionManager.RequestNavigate("TabRegion", new Uri("Search?X=1", UriKind.Relative));

在选项卡控件中创建新选项卡并显示视图.该视图包含多个子区域(以及每个区域的视图).到目前为止,这有效.当用户再次单击该按钮时,我收到错误消息,指出区域已注册.

The new tab is created in the tab control and displays the view. The view contains multiple sub regions (and a view for each of those regions). This works so far. When the user clicks the button again, I get the error saying that the regions are already registered.

我知道这是因为这些区域无法在同一个区域管理器中再次注册.我读过可以使用作用域区域来解决这个问题.尽我所能,我用这个替换上面的 RequestNavigate 来创建一个有作用域的 RegionManager:

I know this is happening because the regions cannot be registered again in the same region manager. I've read that scoped regions can be used to resolve this. As best as I can figure out, I replace the RequestNavigate above with this to create a scoped RegionManager:

SearchViewModel svm = new SearchViewModel();
IRegion detailsRegion = _regionManager.Regions["TabRegion"];
SearchView view = new SearchView(svm);
IRegionManager scopedRegionManager = detailsRegion.Add(view, null, true);
svm.ScopedRegionManager = scopedRegionManager;
svm.LoadViews();

我相信我必须在方法 LoadViews() 中手动将视图加载到我的子区域中,但是子区域没有显示.

I believe I have to manually load the views into my sub regions in the method LoadViews(), but the sub regions are not displaying.

以下是它们在 Search.xaml 中的定义:

Here is how they are defined in Search.xaml:

<ContentControl Background="White" Grid.Row="0"  Grid.Column="0"        
Regions:RegionManager.RegionName="SubRegion1"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"  />

首先,scopedRegionManager 的区域集合中似乎没有我的任何子区域.

As a starting point, the scopedRegionManager does not seem to have any of my sub regions in it's region collection.

编辑

我现在看到作用域区域管理器的区域集合中的子区域.子视图仍未显示,这是我尝试在 LoadViews 方法中加载子视图的方式,这可能完全偏离:

I am now seeing the sub regions in the region collection of the scoped region manager. The sub views are still not displaying, here is how I am trying to load the sub views inside the LoadViews method, which might be completely off course:

ViewModelSub1 vm = new ViewModelSub1();
IRegion detailsRegion = _regionManager.Regions["RegionSub1"];
SubView1 sView = new SubView1(vm);
IRegionManager detailsRegionManager = detailsRegion.Add(sView, null, false);
detailsRegion.Activate(wsView);

我是 WPF 和 MVVM 的新手.任何帮助或正确方向的指示将不胜感激,因为我完全迷失了.

I'm new to WPF and MVVM. Any assistance or pointers in the right direction would be greatly appreciated as I'm utterly lost.

谢谢.

推荐答案

我喜欢使用 Prism 框架,但是我觉得他们的 Regions 应该只用于应用程序布局 (MenuRegion, NavigationRegionContentRegion 等),而不是导航.将它们用于导航意味着让 View 控制应用程序流程,我觉得这是 ViewModel 的工作.

I like using the Prism framework, however I feel that their Regions should only be used for Application Layout (MenuRegion, NavigationRegion, ContentRegion, etc), and not Navigation. Using them for Navigation means letting the View control the application flow, and I feel that is a job for the ViewModels.

要创建类似您概述的内容,我通常有一个包含

To create something like you outlined, I usually have a parent ViewModel which contains

  • ObservableCollection标签
  • int SelectedTabIndex
  • ICommand AddNewTabCommand

我的 TabControl 定义如下所示:

My TabControl definition looks like this:

<TabControl ItemsSource="{Binding Tabs}" SelectedIndex="{Binding SelectedTabIndex}" />

AddNewTab 命令只是将一个新对象添加到 Tabs 集合并将其设置为 Selected

and the AddNewTab command simply adds a new object to the Tabs collection and sets it as Selected

void AddNewTab()
{
    var newTab = new TabAViewModel();
    Tabs.Add(newTab);
    SelectedTabIndex = Tabs.IndexOf(newTab);
}

为了确定显示什么View,我通常使用DataTemplates

To determine what View to display, I usually use DataTemplates

<DataTemplate TargetType="{x:Type local:TabAViewModel}">
    <local:TabAView />
</DataTemplate>

<DataTemplate TargetType="{x:Type local:TabBViewModel}">
    <local:TabBView />
</DataTemplate>

这篇关于区域已在导航上注册错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆