Winform之MVP模式

引用WinFormsMvp.dll

项目结构

Winform之MVP模式_第1张图片


TestModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MVPTest01
{
    public class TestModel
    {
       public string aa { get; set; }
    }
}


TestPresenter.cs

using MVPTest01.View;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using WinFormsMvp;

namespace MVPTest01
{
    public class TestPresenter : Presenter, IDisposable
    {

        public TestPresenter(ITestView view)
            : base(view)
        {
            view.ViewLoad += view_ViewLoad;
        }
        void view_ViewLoad(object sender, EventArgs e)
        {
            try
            {
                View.Model = new TestModel();
                View.Model.aa = "111";
                View.Message("取消归档申请处理成功!");
                View.BindingData(View.Model.aa);
            }
            catch (Exception ex)
            {View.Message(ex.Message);
            }
        }

        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }
}

ITestView.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinFormsMvp;


namespace MVPTest01.View
{
    public interface ITestView : IView
    {
        void BindingData(string data);
        /// 
        /// 询问信息
        /// 
        /// 
        /// 
        Boolean MessageAsk(string text);
        /// 
        ///提示信息
        /// 
        /// 
        void Message(string text);
    }
}

窗体代码

using MVPTest01.View;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using WinFormsMvp.Binder;
using WinFormsMvp.Forms;

namespace MVPTest01
{
    [WinFormsMvp.PresenterBinding(typeof(TestPresenter))]

    public partial class FrmTest : Form, ITestView
    {
        public FrmTest()
        {
            InitializeComponent();
        }
        private readonly PresenterBinder _presenterBinder = new PresenterBinder();
        public bool ThrowExceptionIfNoPresenterBound { get; private set; }
        private void FrmTest_Load(object sender, EventArgs e)
        {
            ThrowExceptionIfNoPresenterBound = true;
            _presenterBinder.PerformBinding(this);
            if (ViewLoad != null)
            {
                        ViewLoad(this, new EventArgs());
            }

        }
        public event EventHandler ViewLoad;

        public bool MessageAsk(string text)
        {
            if (MessageBox.Show(text, @"确认", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk,
                MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                return true;
            }
            return false;
        }

        public void Message(string text)
        {
            MessageBox.Show(text, @"提示");
        }

        void ITestView.BindingData(string testmodel)
        {
            this.Invoke(new ThreadStart(delegate
              {

                  label1.Text = testmodel;
              }));
        }

        public TestModel Model
        {
            get;
            set;
        } 
    }
}


效果图

Winform之MVP模式_第2张图片

Winform之MVP模式_第3张图片


你可能感兴趣的:(DotNet_框架)