在 Prism 中使用 Presenter

发布于 2024-11-27 23:16:16 字数 1216 浏览 9 评论 0原文

我正在 C# 和 WPF 中使用 PRISM 开发一个应用程序。 我对此很陌生,想实现 Presenter。基本上,我想在我的模块中注册一个演示者而不是视图。

目前我正在模块初始化中执行以下操作:

iRegionManager.RegisterViewWithRegion("MainRegion", typeof(AboutWindow));

我想要的是我想要一个演示者,我将在我的模块中注册演示者。这位主持人必须负责展示我所在地区的观点。

我尝试阅读几篇文章和示例,但无法准确获得我想要的内容。

我的要求的伪代码如下:

public class AboutModule : IAboutModule
{
    IRegionManager iRegionManager = null;
    IUnityContainer container = null;

    public AboutModule(IRegionManager iRegionManager, IUnityContainer container)
    {
        this.iRegionManager = iRegionManager;
        this.container = container;
    }

    public void Initialize()
    {
        //Register my presenter here.
    }
}


internal class AboutModulePresenter : IAboutModulePresenter
{
    private IAboutModuleView iAboutModuleView = null;

    internal AboutModulePresenter(IAboutModuleView iAboutModuleView)
    {
        this.iAboutModuleView = iAboutModuleView;
    }
    public IAboutModuleView View
    {
        get
        {
            return this.iAboutModuleView;
        }
    }
    public void ShowView()
    {
        //Register my view with region manager and display in the region.
    }
}

I am developing an application using PRISM in C# and WPF.
I am new to this and would like to implement the Presenter. Basically, I would like to register a Presenter instead of View in my Module.

At present I am doing the following in my Module Initialize:

iRegionManager.RegisterViewWithRegion("MainRegion", typeof(AboutWindow));

What I would like is I want to have a presenter, I will register presenter in my module. This presenter must be responsible to show the view in my region.

I tried reading several articles and examples but was not able to get exactly what I want.

Pseudo-code for my requirements is as follows:

public class AboutModule : IAboutModule
{
    IRegionManager iRegionManager = null;
    IUnityContainer container = null;

    public AboutModule(IRegionManager iRegionManager, IUnityContainer container)
    {
        this.iRegionManager = iRegionManager;
        this.container = container;
    }

    public void Initialize()
    {
        //Register my presenter here.
    }
}


internal class AboutModulePresenter : IAboutModulePresenter
{
    private IAboutModuleView iAboutModuleView = null;

    internal AboutModulePresenter(IAboutModuleView iAboutModuleView)
    {
        this.iAboutModuleView = iAboutModuleView;
    }
    public IAboutModuleView View
    {
        get
        {
            return this.iAboutModuleView;
        }
    }
    public void ShowView()
    {
        //Register my view with region manager and display in the region.
    }
}

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

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

发布评论

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

评论(2

随遇而安 2024-12-04 23:16:16

你可以这样做。本质上,您必须使用您正在使用的任何 IoC 容器(例如 Unity)将 IAboutModuleView 映射到 AboutModuleView。然后在 ShowView 方法中,您将调用 RegionManager 上的 RegisterViewWithRegion,并传入视图。

然而,您将如何以及在哪里构建您的演示者?谁负责调用 ShowView()?

我还建议您查看 MVVM 模式(是否使用 VM 优先或视图优先取决于您),它与 MVP 非常相似,但更适合 WPF 和 Silverlight 应用程序。

You could do this. Essentially you would have to map IAboutModuleView to AboutModuleView with whatever IoC container you're using e.g. Unity. Then in your ShowView method you would call RegisterViewWithRegion on the RegionManager, passing in the view.

However, how and where would you construct your presenter? Who would be responsible for calling ShowView()?

I would also recommend taking a look at the MVVM pattern (whether you use VM-first or View-first is up to you) which is fairly similar to MVP but is better suited to WPF and Silverlight applications.

十二 2024-12-04 23:16:16

要显示或隐藏某个区域中的视图,您可以自行添加或删除视图:

void AddView()
{
    IRegion region = this._regionManager.Regions["RegionName"];

    object presentView = region.GetView( "ViewName" );
    if ( presentView == null )
    {
        var view = _container.Resolve<View>( );
        region.Add( view, "ViewName" );
    }
}

void RemoveView()
{
    IRegion region = this._regionManager.Regions["RegionName"];

    object presentView = region.GetView( "ViewName" );
    if ( presentView != null )
    {
        region.Remove( presentView );
    }
}

To show or hide a view in a region you can add or remove the view on your own:

void AddView()
{
    IRegion region = this._regionManager.Regions["RegionName"];

    object presentView = region.GetView( "ViewName" );
    if ( presentView == null )
    {
        var view = _container.Resolve<View>( );
        region.Add( view, "ViewName" );
    }
}

void RemoveView()
{
    IRegion region = this._regionManager.Regions["RegionName"];

    object presentView = region.GetView( "ViewName" );
    if ( presentView != null )
    {
        region.Remove( presentView );
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文