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.
    }
}
4

2 回答 2

0

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.

于 2011-08-04T09:44:57.677 回答
0

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

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 );
    }
}
于 2011-08-08T09:41:59.030 回答