how Implement usercontrol in winforms mvp pattern?

前端 未结 2 783
孤独总比滥情好
孤独总比滥情好 2020-12-31 23:47

I want to Implement MVP pattern. I have a user control that has some Textboxes and when I put it in form I call a method from usercontrol and fill textboxes. But in mvp patt

2条回答
  •  佛祖请我去吃肉
    2021-01-01 00:14

    Here is an example implementation of the pattern. The Presenter only knows about the interface having a show method. The Presenter calls it, but the only the form (aka View) implements how the form should be displayed.

    public interface IMyFormView {
        void Show();
    }
    
    public class MyForm : IMyFormView {
    
        public MyForm() {
            var presenter = new MyFormPresenter(this);
            presenter.Init();
        }
    
        public void Show() {
            usercontrol1.fill();
        }
    }
    
    public class MyFormPresenter
    {
        private IMyView _view;
        public MyFormPresenter(IMyView view) {
            _view = view;
        }
    
        public void Init() {
            _view.Show();
        }
    }
    

    If you need to pass data into the view, then you can pass a view model through the Show-method or set custom Properties on the view.

提交回复
热议问题