在 Prism (CAL) 中,如何使用 RegisterPresenterWithRegion 而不是 RegisterViewWithRegion
我在 Prism 应用程序中有一个模块,在其初始化方法中,我想注册一个演示者而不是一个视图到一个区域,即我想这样做:
PSEUDO-CODE:
regionManager.RegisterPresenterWithRegion(
"MainRegion", typeof(Presenters.EditCustomerPresenter));
而不是加载这样的视图:
regionManager.RegisterViewWithRegion(
"MainRegion", typeof(Views.EditCustomerView));
演示者当然会带来自己的视图并最终在该区域中注册该视图,但它允许我将演示者绑定到演示者的构造函数中的视图,而不是绑定两者在 XAML 中一起使用(这更像是一种解耦的 MVVM 模式,我想在此处避免)。
如何将演示者添加到区域而不是视图?
namespace Client.Modules.CustomerModule
{
[Module(ModuleName = "CustomerModule")]
public class CustomerModule : IModule
{
private readonly IRegionManager regionManager;
public CustomerModule(IRegionManager regionManager)
{
this.regionManager = regionManager;
}
public void Initialize()
{
regionManager.RegisterViewWithRegion("MainRegion", typeof(Views.EditCustomerView));
}
}
}
I have a module in a Prism application and in its initialize method I want to register a presenter instead of a view with a region, i.e. I want to do this:
PSEUDO-CODE:
regionManager.RegisterPresenterWithRegion(
"MainRegion", typeof(Presenters.EditCustomerPresenter));
instead of loading a view like this:
regionManager.RegisterViewWithRegion(
"MainRegion", typeof(Views.EditCustomerView));
The presenter would of course bring along its own view and ultimately register this view in the region, but it would allow me to bind the presenter to the view in the presenter's constructor instead of binding the two together in XAML (which is more of a decoupled MVVM pattern which I want to avoid here).
How can I add a Presenter to a Region instead of a view?
namespace Client.Modules.CustomerModule
{
[Module(ModuleName = "CustomerModule")]
public class CustomerModule : IModule
{
private readonly IRegionManager regionManager;
public CustomerModule(IRegionManager regionManager)
{
this.regionManager = regionManager;
}
public void Initialize()
{
regionManager.RegisterViewWithRegion("MainRegion", typeof(Views.EditCustomerView));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为您的演示者应该负责在激活它们时将它们插入该区域。我通常创建一个 IViewRegistry 供演示者使用,以避免他们了解区域名称并让他们的演示者使用它来显示视图。
当然,
ShowInMainRegion
的实现将是您已有的区域注册表代码。您有可能做一些更像您想要的事情(也许是检测
IViewFactory
的区域适配器),但这可能不切实际。希望这有帮助,
安德森
I think your presenters should be responsible for inserting them into the region when they are activated. I usually create an IViewRegistry for my presenters to use that avoids them knowing about region names and have their presenters use this to show the view.
And then of course, the implementation of
ShowInMainRegion
would be that region registry code you already have.There is a possibility you could do something that is more like what you want (a region adapter that detects an
IViewFactory
, maybe), but it's probably not practical.Hope this helps,
Anderson
我对 Prism 还很陌生,但据我了解,这没有意义:区域旨在保存视图,不是吗?这就是它们存在的全部目的。您希望从您的区域获得什么供您的演示者使用?
鉴于您的 Presenter 了解有关您的 View 的所有信息,您可以在 RegisterViewWithRegion 调用中使用您的 Presenter:
I'm still quite new to Prism but as I understand it, that doesn't make sense: Regions are designed to hold Views, aren't they. That's all they exist for. What are you hoping to get from your Region that your Presenter can use?
Given that your Presenter knows all about your View, can you use your Presenter in your RegisterViewWithRegion call:
您可以尝试使用 RegisterViewWithRegion 重载,该重载采用委托而不是视图类型。
例如:
这将允许您拥有自己的自定义逻辑来创建视图/视图模型(也称为演示者)。当找到指定区域时,将调用回调。
You can try using the RegisterViewWithRegion overload that takes a delegate instead of a view type.
For example:
This will allow you to have your own custom logic for creating the view/viewmodel(aka presenter). The callback will be called when the named region is found.
我的方法是通过将已解析演示者的 View 属性传递给该视图来将视图注册到该区域。
我的演示者构造函数将如下所示:
视图和演示者之前都已在容器中注册。
My approach to this is to register the view with the region by passing it the View property of a resolved presenter
My presenters constructor would look like this:
Both the view and presenter have previously been registered in the container.