在 Prism (CAL) 中,如何使用 RegisterPresenterWithRegion 而不是 RegisterViewWithRegion

发布于 2024-08-03 19:53:17 字数 1051 浏览 3 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(4

黎夕旧梦 2024-08-10 19:53:17

我认为您的演示者应该负责在激活它们时将它们插入该区域。我通常创建一个 IViewRegistry 供演示者使用,以避免他们了解区域名称并让他们的演示者使用它来显示视图。

public class MyViewPresenter : IPresenter
{
     IViewRegistry _viewReg;
     IUnityContainer _container;
     public MyViewPresenter(IViewRegistry viewRegistry, IUnityContainer container)
     {
          _viewReg = viewRegistry;
          _container = container;
     }

     void IPresenter.Present()
     {
          MyView view = _container.Resolve<MyView>();
          MyViewViewModel vm = _container.Resolve<MyViewViewModel>();
          view.DataContext = vm;

          _viewReg.ShowInMainRegion(view);
     }

}

当然,ShowInMainRegion 的实现将是您已有的区域注册表代码。

public void ShowInMainRegion(object view)
{
     regionManager.RegisterViewWithRegion(
        "MainRegion", view);
}

您有可能做一些更像您想要的事情(也许是检测 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.

public class MyViewPresenter : IPresenter
{
     IViewRegistry _viewReg;
     IUnityContainer _container;
     public MyViewPresenter(IViewRegistry viewRegistry, IUnityContainer container)
     {
          _viewReg = viewRegistry;
          _container = container;
     }

     void IPresenter.Present()
     {
          MyView view = _container.Resolve<MyView>();
          MyViewViewModel vm = _container.Resolve<MyViewViewModel>();
          view.DataContext = vm;

          _viewReg.ShowInMainRegion(view);
     }

}

And then of course, the implementation of ShowInMainRegion would be that region registry code you already have.

public void ShowInMainRegion(object view)
{
     regionManager.RegisterViewWithRegion(
        "MainRegion", view);
}

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

心碎的声音 2024-08-10 19:53:17

我对 Prism 还很陌生,但据我了解,这没有意义:区域旨在保存视图,不是吗?这就是它们存在的全部目的。您希望从您的区域获得什么供您的演示者使用?

鉴于您的 Presenter 了解有关您的 View 的所有信息,您可以在 RegisterViewWithRegion 调用中使用您的 Presenter:

regionManager.RegisterViewWithRegion(
    "MainRegion", typeof(Presenters.EditCustomerPresenter.View));

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:

regionManager.RegisterViewWithRegion(
    "MainRegion", typeof(Presenters.EditCustomerPresenter.View));
沫雨熙 2024-08-10 19:53:17

您可以尝试使用 RegisterViewWithRegion 重载,该重载采用委托而不是视图类型。

例如:

regionManager.RegisterViewWithRegion(RegionNames.APPLICATION_MANAGEMENT_REGION, OnGetManagementView);

public object OnGetManagementView()
{
     return m_managementViewModel.View;
}

这将允许您拥有自己的自定义逻辑来创建视图/视图模型(也称为演示者)。当找到指定区域时,将调用回调。

You can try using the RegisterViewWithRegion overload that takes a delegate instead of a view type.

For example:

regionManager.RegisterViewWithRegion(RegionNames.APPLICATION_MANAGEMENT_REGION, OnGetManagementView);

public object OnGetManagementView()
{
     return m_managementViewModel.View;
}

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.

鲜肉鲜肉永远不皱 2024-08-10 19:53:17

我的方法是通过将已解析演示者的 View 属性传递给该视图来将视图注册到该区域。

this.regionManager.RegisterViewWithRegion(
    FoundationToolkitRegionNames.RIBBON_REGION, 
    () => this.container.Resolve<SetupRibbonTabPresenter>().View);

我的演示者构造函数将如下所示:

public SetupRibbonTabPresenter(ISetupRibbonTabView view)
{
    this.view = view;
}

视图和演示者之前都已在容器中注册。

My approach to this is to register the view with the region by passing it the View property of a resolved presenter

this.regionManager.RegisterViewWithRegion(
    FoundationToolkitRegionNames.RIBBON_REGION, 
    () => this.container.Resolve<SetupRibbonTabPresenter>().View);

My presenters constructor would look like this:

public SetupRibbonTabPresenter(ISetupRibbonTabView view)
{
    this.view = view;
}

Both the view and presenter have previously been registered in the container.

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