【发布时间】:2013-08-07 19:33:22
【问题描述】:
我在我的 WPF MVVM 应用程序中使用Prism 进行导航。我注册我的观点如下。
// MyView is the data type of the view I want to register and "MyView"
// is the name by which I want the data type to be identified within
// the IoC container.
_container.RegisterType<object, MyView>("MyView");
我将这个视图显示如下。
_regionManager.RequestNavigate(
"MyRegion", // This is the name of the Region where the view should be displayed.
"MyView" // This is the registered name of the view in the IoC container.
);
在应用程序的其他地方,我需要在事件处理程序中删除此视图;然而,下面的代码返回一个ArgumentNullException。
_regionManager.Regions["MyRegion"].Remove(
_regionManager.Regions["MyRegion"].GetView("MyView")
);
这表明RequestNavigate 方法没有使用名称“MyView”将MyView 添加到MyRegion。我知道如果我使用_regionManager.Add(MyView, "MyView") 方法,GetView 方法不会返回 null。不幸的是,RequestNavigate 似乎并没有以相同的方式处理视图名称。当使用RequestNavigate 方法添加视图时,有什么方法可以从区域中删除视图(按名称)?
【问题讨论】:
-
在深入了解 Prism 源代码后,
RequestNavigate似乎最终调用了region.Add(view);(在RegionNavigationContentLoader.LoadContent中下降)。所以就目前而言,我认为我想做的事情是不可能的。我认为可以通过使用用于指定视图名称的附加参数重载各种RequestNavigate方法和扩展来实现此功能。这将允许通过名称访问已使用RequestNavigate添加到区域的视图。 -
我已将您的场景明确添加到我的答案中。
标签: c# wpf mvvm navigation prism