Prism 2 SL:单击按钮时从区域中删除视图

发布于 2024-07-16 03:00:15 字数 228 浏览 5 评论 0原文

我是 Prism 的新手,我正在尝试确定在 Prism 2 应用程序中停用视图的最佳实践 - 当用户单击视图上的按钮时,我想要停用该视图。 单击按钮时视图正在执行命令。

视图模型正在接收命令,但视图模型没有对区域管理器的引用。

视图模型应该了解区域管理器吗? 即使视图模型有对它的引用,它也需要将视图实例传递给包含区域上的非活动方法。

我正在为应用程序使用 MVVM 模式。

I am new to Prism and I am trying to determine a best practice for deactivating a view in a Prism 2 application -- when a user clicks a button on the view I want to deactivate the view. The view is executing a command when the button is clicked.

The view model is receiving the command but the viewmodel does not have a reference to the regionmanager.

Should the view model know about the region manager? And even if the viewmodel had a reference to it, it needs the view instance to pass to the deactive method on the containing region.

I am using the MVVM pattern for the app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋叶绚丽 2024-07-23 03:00:15

我将在这里冒险并假设您正在为您的区域使用项目控件或类似控件,因为这是“删除视图的按钮”最明显的用法。

在这种情况下,您应该在某个地方有一个控制器(或者您想称呼它的任何名称,但 Prism 团队似乎使用这种名称来承担这种责任)负责管理活动 ViewModel,并且您可以显示您的视图使用 DataTemplates,或者在添加新实例时手动创建/添加/激活新视图。
您的情况似乎是第二种情况,控制器应该负责管理视图。 您的视图模型应该通过依赖注入获得对该控制器的引用,并要求它将其从活动模型/视图池中删除。

Controller本身接收IRegionManager并找到它负责的Region。

我希望这是有道理的,如果没有,请评论。

I'm gonna go on a limb here and assume that you are using an itemscontrol or such for your region, because that's the most obvious use of a "button that removes a view".

In that case, you should have a Controller (or whatever you want to call it, but the Prism team seems to use that kind of name for that kind of responsibility) somewhere in charge of managing the active ViewModels, and either you display your views using DataTemplates, or you manually create/add/activate a new view when adding a new instance.
Your case seems to be the second one, and the controller should be responsible for managing the views. Your viewmodel should get a reference to that controller through Dependency Injection and ask it to remove it from the pool of active models/views.

The Controller itself receives the IRegionManager and finds the Region it is responsible for.

I hope that makes sense, please comment if it does not.

终弃我 2024-07-23 03:00:15

在您的情况下,EventAggregator(需要卸载自身的控件)可能是一种矫枉过正,因为简单地将IRegionManager注入到该控制视图模型构造函数中

ctor (IRegionManager regionManager)
{
    this.RegionManager = regionManager
}

,然后类似这样的事情

this.regionManager.Regions[regionName].Remove(this.View);

应该可以完成你要求的。

IRegionManager 是可模拟的接口,可以轻松测试,它只是一个抽象,不会将您耦合到实现和启用 IoC。

EventAggregator in your case (control which needs to unload itself) is maybe an overkill because simple injecting of IRegionManager to that control view model constructor

ctor (IRegionManager regionManager)
{
    this.RegionManager = regionManager
}

and then something like this

this.regionManager.Regions[regionName].Remove(this.View);

should do the thing you asked for.

IRegionManager is mockable interface enabling easy testing and it is just an abstraction not coupling you to implementation and enabling IoC.

清风不识月 2024-07-23 03:00:15

我这里也挂了 我发现,由于 RegionManager 负责它包含的区域,因此我的 RegionManager 和我想要管理的区域之间出现了紧密耦合。

从外部管理这些区域(例如从主应用程序菜单)是一项复杂的工作,因此我们放弃了 PRISM 框架,并根据在我们的特定场景中使用的复合应用程序指导元素编写了我们自己的代码。

I got hung up here too. I found that because the RegionManager was responsible for the Regions it contained I was ending up with tight coupling between my RegionManager and the Regions I wanted to manage.

It was a complex job to manage these regions externally (from say a main application menu) and as a result we dropped the PRISM framework and wrote our own code based on the Composite Application Guidance elements that were of use in our particular scenario.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文