在我的应用程序中,我使用的是Prism,它通过发现(Unity)加载了三个模块。
加载模块时,它们会在“TopLeftRegion”区域中注册每个视图。
我正在尝试执行模块视图的导航。我的意思是,创建Before
和Next
方法,可以激活(或["TopLeftRegion"].Activate(..)
)该区域中的当前视图。
例如,想象一下:
|_ModuleA
|
|_ModuleB
|
|_ModuleC
如果我当前的观看次数为ModuleA
,如果我按Next
,则必须显示ModuleB
,如果我按Before
,则必须在该地区显示ModuleA
。
我在看房子:
regionManager.Regions["TopLeftRegion"].Views
但我不知道这样做。属性View
不允许访问数据并进入数据。
这是一个简单的项目,我正在尝试在ShellViewModel中创建该方法,但我没有得到它。如何导航每个模块?
答案 0 :(得分:1)
Prism并不假设区域中只有一个活动视图,因此Region类上没有任何属性可以使这个超级简单。但它并不太棘手。
RegionManager类跟踪ActiveViews属性中哪些视图处于活动状态。它不跟踪哪个视图处于活动状态。在您的情况下,您所在的区域仅支持一个活动视图,因此您只需找到该集合中的第一个视图即可。
另一个棘手的部分是在Region.Views集合中查找活动视图。下面我将Region.Views转换为List,以便我可以使用FindIndex来定位该活动视图的索引。
private void Next(object commandArg)
{
IRegion myRegion = _regionManager.Regions["TopLeftRegion"];
object activeView = myRegion.ActiveViews.FirstOrDefault(); //Here we're trusting that nobody changed the region to support more than one active view
List<object> myList = myRegion.Views.ToList<object>(); //Cast the list of views into a List<object> so we can access views by index
int currentIndex = myList.FindIndex(theView => theView == activeView);
int nextIndex = (currentIndex + 1) % (myList.Count); //Wrap back to the first view if we're at the last view
object nextView = myList[nextIndex];
myRegion.Activate(nextView);
}
导航到上一个视图将大致相同,除非您从索引中减去一个而不是添加一个。