Winforms MVP显示mdi子表单

时间:2016-07-18 21:51:10

标签: c# .net winforms mvp

伙计我有MVP设计模式的问题因为我不知道如何以父表格显示子视图。 我的观点没有MdiParent属性。我可以在视图界面中手动创建它吗?

在新窗口中打开每个表单的丑陋外观!

我有两位主持人:

  1. MainPresenter(代表mainForm(父)逻辑)
  2. TaskPresenter(表示保存,插入,删除逻辑的逻辑)
  3. 两个View界面:

    1. IMainView
    2. ITaskView
    3. 两个winforms:

      1. MainForm - mainwindow(parrent mdi form)
      2. TaskForm
      3. 检查代码:

        MainPresenter

        public class MainPresenter
        {
            private readonly IMainView view;
        
            private List<ITaskModel> tasks;
        
            // Constructor
            public MainPresenter(IMainView view)
            {
                this.view = view;
                this.Init();
                this.tasks = new List<ITaskModel>();
            }
        
            // Initialize
            private void Init()
            {
                this.view.AddTask += AddTask;
            }
        
            // Add task
            private void AddTask(object sender, EventArgs e)
            {
                // Show as MDI CHILD 
            }
        }
        

        IMainView

          public interface IMainView 
          {
                event EventHandler<EventArgs> AddTask;
          }
        

        TaskPresenter

        public class TaskPresenter 
        {
            private readonly ITaskView view;
            private List<ITaskModel> tasks;
        
            private bool isNew = true;
            private int currentIndex = 0;
        
            // Constructor
            public TaskPresenter(ITaskView view)
            {
                this.view = view;
                this.Initialize();
            }
        
            // Initialize
            public void Initialize()
            {
                tasks = new List<ITaskModel>();
        
                view.SaveTask += Save;
                view.NewTask += New;
                view.PrevTask += Previous;
                view.NextTask += Next;
            }
        
            private void Save(object sender, EventArgs e)
            {
            }
            private void New(object sender, EventArgs e)
            {
            }     
            private void Next(object sender, EventArgs e)
            {
            }
            private void Previous(object sender, EventArgs e)
            {
            }
            private void BlankTask()
            {   
            }
            private void LoadTask(ITaskModel task)
            { 
            }
        }
        

        ITaskView

        public interface ITaskView
        {
            String TaskName { get; set; }
            String TaskPriority { get; set; }
            DateTime? StartDate { get; set; }
            DateTime? DuoDate { get; set; }
        
            event EventHandler<EventArgs> SaveTask;
            event EventHandler<EventArgs> NewTask;
            event EventHandler<EventArgs> NextTask;
            event EventHandler<EventArgs> PrevTask;
        }
        

        这是MainForm

        public partial class MainForm : Form, IMainView
        {
            MainPresenter Presenter;
        
            // Construcor
            public MainForm()
            {
                InitializeComponent();
            }
        
            // Events
            public event EventHandler<EventArgs> AddTask;
        
            // On load
            private void MainForm_Load(object sender, EventArgs e)
            {
                Presenter = new MainPresenter(this);
            }
        
            // On click add task btn
            private void addTaskBtn_Click(object sender, EventArgs e)
            {
                if(AddTask != null)
                {
                    // When is this event triggered i want to show another child form for adding new task
                    AddTask(this, EventArgs.Empty); 
                }
            }
        }
        

        那么如何在TaskView中将MainView视为孩子呢? enter image description here

1 个答案:

答案 0 :(得分:0)

UIApplication它是Windows Forms Application但输出类型Class Library。 添加参考MVPFramework

MainForm

public partial class MainForm : Form , IMainView
{
    [Resolve]
    public IMainControl mainControl;

    public MainForm()
    {
        InitializeComponent();
    }


    public bool ShowAsDialog()
    {
        throw new NotImplementedException();
    }

    private void openChildFormToolStripMenuItem_Click(object sender, EventArgs e)
    {
        mainControl.OnOpenChildForm();
    }
}

儿童表格

public partial class ChildForm : Form , IChildView
    {
        public ChildForm()
        {
            InitializeComponent();
        }

        public bool ShowAsDialog()
        {
            throw new NotImplementedException();
        }


        public object MDIparent
        {
            set
            {
                this.MdiParent = (Form)value;
            }
        }
    }

CoreApplication是类库

IMainControl

 public interface  IMainControl :IControl
  {
      void OnOpenChildForm();
  }

MainControl

public class MainControl  :IMainControl
    {
        [Resolve]
      public IApplicationController applicationController;
      public void OnOpenChildForm()
      {
       IChildControl TransfersOnTheWayControl = applicationController.Resolve<IChildControl>();
       TransfersOnTheWayControl.Run();
      }
    }

IChildControl

 public interface IChildControl :IControl
      {
         void Run();
      }

IMainView

public interface  IMainView :IView
{

}

IChildView

public interface IChildView:IView       {

      bool ShowAsDialog();`enter code here`

      object MDIparent { set; }
  }

IMainPresenter

public interface  IMainPresenter :IPresenter
 {

 }

IChildPresenter

public interface  IChildPresenter :IPresenter
 {
   bool Ask();
 }

MainPresenter

public class MainPresenter :BasePresenter<IMainView>, IMainPresenter
 {

 }

ChildPresenter

public class ChildPresenter : BasePresenter<IChildView>, IChildPresenter
    {

       public bool Ask()
       {
            this.Init();
            bool res = View.ShowAsDialog();
            ApplicationController.ClearInstance<IChildView>();
            return res;
       }

        public override void Init()
        {
            View.MDIparent = ApplicationController.GetMainFrom<IMainPresenter>();
            base.Init();
        }
    }

LauncherApplication它是控制台应用程序,但输出类型为Windows Form。

 class Program
        {
            static void Main(string[] args)
            {
                IApplicationController applicationController = new ApplicationController(new ServicesContainerAdapter());

                applicationController


                    //RegisterView
                    .RegisterView<IMainView, MainForm>()
                    .RegisterView<IChildView, ChildForm>()


                    //RegisterPresenter
                    .RegisterPresenter<IMainPresenter, MainPresenter>()
                    .RegisterPresenter<IChildPresenter, ChildPresenter>()


                    //RegisterController
                    .RegisterController<IMainControl, MainControl>()
                    .RegisterController<IChildControl, ChildControl>();



                IMainPresenter mainPresenter = applicationController.Resolve<IMainPresenter>();
                mainPresenter.Init();

                Application.Run((Form)mainPresenter.FormObject);
            }
       }