Caliburn笔记-Presenter(wpf框架)

    又是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本身定义了方法与事件,但并不参与生命流程的制定.
WPF(Windows Presentation Foundation)是一种用于创建Windows桌面应用程序的用户界面框架,而Caliburn.Micro是一种针对WPF应用程序的轻量级MVVM框架。通过结合使用这两个框架,可以更高效地编写WPF应用程序的界面和逻辑部分。 一个基本的WPF Caliburn.Micro框架编写案例可以是一个简单的待办事项列表应用程序。首先,我们可以创建一个WPF应用程序,并添加Caliburn.Micro框架的引用。然后,我们可以定义待办事项的模型,包括待办事项的名称、描述和状态等属性。 接下来,我们可以使用Caliburn.Micro框架提供的ViewModelBase类来创建一个待办事项的视图模型。在视图模型中,我们可以定义与界面交互的属性和命令,比如显示待办事项列表、添加新的待办事项、标记已完成的待办事项等功能。 然后,我们需要创建一个WPF的视图,用于显示待办事项列表和与用户进行交互。在视图中,我们可以绑定视图模型中的属性和命令,并使用WPF的控件来显示和编辑待办事项。 最后,我们将视图模型绑定到视图上,使得视图与视图模型能够实现双向的数据绑定和命令绑定。这样,当用户在界面上进行操作时,视图和视图模型之间的数据和行为可以实现同步。 通过这样一个简单的案例,我们可以看到WPF Caliburn.Micro框架编写的优势,它能够帮助我们更快速和高效地构建WPF应用程序,并实现良好的界面和逻辑分离。同时,借助MVVM架构,我们也能够实现更好的代码可读性和可维护性。 WPF Caliburn.Micro框架编写案例的实践将有助于我们更好地理解和运用这些框架,提升WPF应用程序的开发效率和质量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值