复合视图 - 视图已存在于区域错误中
我正在开发 WPF/Prism 4/AvalonDoc 应用程序。
我的一个观点非常复杂,ViewModel 变得巨大且难以管理。所以我决定将其拆分为具有自己的 ViewModel 的较小视图。
我在视图上放置了一个区域,并使用“主”ViewModel 构造函数中的 RegionManager.RegisterViewWithRegion 方法将子视图加载到该区域中。
当仅加载视图的一个实例时,这就像预期的那样工作。但是当我打开该屏幕的第二个实例(它作为 DocumentContent 加载到 AvalonDock 的 DocumentPane 中)时,我收到“视图已存在于区域中”错误。
如果可能的话,我想避免给该地区起一些独特的名称。 有没有办法表明我正在将子视图添加到“主”视图的特定实例? 关于如何做到这一点有什么建议吗?
I am working on a WPF/Prism 4/AvalonDoc application.
One of my views is very complicated and a ViewModel become huge and unmanagable. So I decided to split it to smaller views with their own ViewModels.
I have placed a region on the view and loading the sub-view into that region using regionManager.RegisterViewWithRegion method from "master" ViewModel constructor.
That works just as expected when only one instance of a view is loaded. But when I am opening second instance of that screen (it is loaded as DocumentContent into AvalonDock's DocumentPane) I am getting "View already exists in region" error.
I would like to avoid giving the region some unique name if possible.
Is there a way to indicate that I am adding sub-view to specific instance of the "master" view?
Any advise on how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 MEF 并且您的视图是 Singleton ie
,并且您的
INavigationAware
在{return false;}
上返回 false,您将收到此错误。
将
[PartCreationPolicy(CreationPolicy.Shared)]
更改为[PartCreationPolicy(CreationPolicy.NonShared)]
即可完成。
If you are using MEF and your view is Singleton i.e.
and your
INavigationAware
return false on{return false;}
You will get this error.
Change
[PartCreationPolicy(CreationPolicy.Shared)]
to[PartCreationPolicy(CreationPolicy.NonShared)]
and you are done.
我担心您不能有多个同名的区域。当视图模型变得复杂时,我不想拆分视图。您可以将视图模型拆分为几个专门的视图模型。
一个例子。你有一个用户管理。一个视图模型是所有用户的列表。该列表包含用户视图模型。每个用户视图模型都包含一个用户权限视图模型的实例,该实例本身也是仅代表一个用户权限的视图模型列表。因此,如何呈现特殊用户权限的逻辑进入对应的视图模型,而不是进入“主”用户列表视图模型。这样您就可以将您的关注点分成不同的视图模型。
绑定到这些视图模型就像只有一个视图模型一样简单。只是路变得有点长了。当数据上下文是上面示例中的用户列表视图模型时,您可能可以使用像 CurrentUserVM.UserRightsVM 这样的绑定路径来获取用户权限列表。
I fear that you cannot have several regions with the same name. I would prefer not to split the views when the view model gets complicated. You can split the view model into several specialized view model.
An example. You have a user management. One view model is the list of all users. This list contains user view models. And each user view model contains an instance of a user rights view model which itself is again a list of view models that represent only one user right. So the logic on how to present a special user right goes into the correspondent view model and not into the "master" user list view model. This way you can separate your concerns into different view models.
Binding to those view models is just as simple as having only one view model. Just the path is getting a bit longer. When the data context is the userlist view model from the example above you could propably have a binding path like CurrentUserVM.UserRightsVM to get the list of user rights.