代码改变世界

Caliburn笔记-Presenter(wpf框架)

  Clingingboy  阅读(833)  评论(0编辑  收藏  举报

    又是MVP...

先来看下图,MetadataContainer已经知道是元数据的功能了。PresenterBase继承了IExtendedPresenter接口,所以重点看这个接口.

参考于此:http://caliburn.codeplex.com/wikipage?title=IPresenter%20Component%20Model&referringTitle=Documentation

image

IExtendedPresenter接口继承了一系列的接口

image

IPresenter包含了一组基本初始化的方法

image

IPresenterNode
IPresenterNode inherits from IPresenter and adds one additional property: Parent. This allows the implementor to communicate directly with its immediate parent. This is most helpful in shutdown scenarios. An example of this can be seen by examining the PresenterBase.Close method.
ILifecycleNotifier
Classes which implement this interface guarantee that they will fire events related to their lifecycle. These events correspond directly to the methods defined by IPresenter. The events are: Initialized, WasShutdown, Activated, Deactivated.
IViewAware
This interface should be implemented when explicit knowledge of the view is needed. If implemented, when the view's Loaded event is fired, the IViewWare.ViewLoaded method will be called. It will be passed the view instance and the context that the view pertains to (because it is possible to have multiple view over the same model).
IPresenterHost
This interface inherits from IPresenter and is implemented by classes which which to manage the lifecycle of other presenter(s). This basically encompasses the ScreenConductor and ScreenCollection roles. The IPresenterHost has a collection of the IPresenters which it is managing called "Presenters" It also adds two important methods:

 

以下为PresenterBase抽象类代码

1.定义IPresenter抽象方法

#region abstract method

/// <summary>
/// Initializes this instance.
/// </summary>
public abstract void Initialize();

/// <summary>
/// Shuts down this instance.
/// </summary>
public abstract void Shutdown();

/// <summary>
/// Activates this instance.
/// </summary>
public abstract void Activate();

/// <summary>
/// Deactivates this instance.
/// </summary>
public abstract void Deactivate();

#endregion


2.定义ILifecycleNotifier接口事件

image

#region Event

/// <summary>
/// Occurs when [initialized].
/// </summary>
public event EventHandler Initialized = delegate { };

/// <summary>
/// Occurs when [was shutdown].
/// </summary>
public event EventHandler WasShutdown = delegate { };

/// <summary>
/// Occurs when [activated].
/// </summary>
public event EventHandler Activated = delegate { };

/// <summary>
/// Occurs when [deactivated].
/// </summary>
public event EventHandler Deactivated = delegate { };

/// <summary>
/// Called when [initialize].
/// </summary>
protected virtual void OnInitialize()
{
    Initialized(this, EventArgs.Empty);
}

/// <summary>
/// Called when [shutdown].
/// </summary>
protected virtual void OnShutdown()
{
    WasShutdown(this, EventArgs.Empty);
}

/// <summary>
/// Called when [activate].
/// </summary>
protected virtual void OnActivate()
{
    Activated(this, EventArgs.Empty);
}

/// <summary>
/// Called when [deactivate].
/// </summary>
protected virtual void OnDeactivate()
{
    Deactivated(this, EventArgs.Empty);
}

#endregion


3.IViewAware接口

/// <summary>
/// Called when the presenter's view is loaded.
/// </summary>
/// <param name="view">The view.</param>
/// <param name="context">The context.</param>
public virtual void ViewLoaded(object view, object context) {}


Presenter为PresenterBase默认实现

/// <summary>
    /// A base implementation of <see cref="IPresenter"/>.
    /// </summary>
    public class Presenter : PresenterBase
    {
        /// <summary>
        /// Initializes this instance.
        /// </summary>
        public override void Initialize()
        {
            if(!IsInitialized)
            {
                OnInitialize();
                IsInitialized = true;
            }
        }

        /// <summary>
        /// Shutdowns this instance.
        /// </summary>
        public override void Shutdown()
        {
            OnShutdown();
        }

        /// <summary>
        /// Activates this instance.
        /// </summary>
        public override void Activate()
        {
            if(!IsActive)
            {
                OnActivate();
                IsActive = true;
            }
        }

        /// <summary>
        /// Deactivates this instance.
        /// </summary>
        public override void Deactivate()
        {
            if(IsActive)
            {
                OnDeactivate();
                IsActive = false;
            }
        }
    }

到此为止,Presenter本身定义了方法与事件,但并不参与生命流程的制定.
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2006-12-28 忙碌的日子暂时停下来了
点击右上角即可分享
微信分享提示