C#事件开始循环

时间:2016-07-20 10:57:38

标签: c# .net winforms mvp

我在MVP视图和演示者中遇到了事件的问题。 当_artistToolbarButton_Click事件被解雇时,他开始向我展示另一个注册事件。为什么会这样?检查动画,你会看到来自另一个事件的antoher消息框也被触发了!

enter image description here

这是我的表单代码:

形式:

 public partial class MainForm : Form, IMainView
    {
        #region Properties

        public IPresenter Presenter { get; set; }

        #endregion

        // Constructor
        public MainForm()
        {
            this.InitializeComponent();
            this.Presenter = new MainPresenter(this);
            this.WireEvents();
        }

        public event EventHandler<EventArgs> OnLoad;
        public event EventHandler<EventArgs> ShowArtist;
        public event EventHandler<EventArgs> ShowLibrary;
        public event EventHandler<EventArgs> ShowTopList;

        // Wire events
        private void WireEvents()
        {
            this.Load += new EventHandler(MainForm_Load);
            this._artistToolbarButton.Click += new EventHandler(_artistToolbarButton_Click);
            this._libraryToolbarButton.Click += new EventHandler(_libraryToolbarButton_Click);
            this._topListToolbarButton.Click += new EventHandler(_topListToolbarButton_Click);
        }

        #region Events
        // Load
        private void MainForm_Load(object sender, EventArgs e)
        {
            if(this.OnLoad != null)
            {
                this.OnLoad(this, EventArgs.Empty);
            }
        }

        // Artist
        private void _artistToolbarButton_Click(object sender, EventArgs e)
        {
            if (this.ShowArtist != null)
            {
                this.ShowArtist(this, EventArgs.Empty);
            }
        }

        // Library
        private void _libraryToolbarButton_Click(object sender, EventArgs e)
        {
            if (this.ShowLibrary != null)
            {
                this.ShowLibrary(this, EventArgs.Empty);
            }
        }

        // Top list
        private void _topListToolbarButton_Click(object sender, EventArgs e)
        {
            if (this.ShowTopList != null)
            {
                this.ShowTopList(this, EventArgs.Empty);
            }
        }
        #endregion
    }

   public class MainPresenter : IPresenter
    {
        private IMainView _view;

        // Constructor
        public MainPresenter(IMainView view)
        {
            this._view = view;
            this.WireEvents();
        }

        // Wire events
        public void WireEvents()
        {
            this._view.OnLoad += OnLoad;
            this._view.ShowArtist += ShowArtist;
            this._view.ShowArtist += ShowLibrary;
            this._view.ShowArtist += ShowTopList;
        }

        // On load
        private void OnLoad(object sender, EventArgs e)
        {
            MessageBox.Show("OnLoad");
        }

        // ShowArtist
        private void ShowArtist(object sender, EventArgs e)
        {
            MessageBox.Show("ShowArtist");
        }

        // ShowLibrary
        private void ShowLibrary(object sender, EventArgs e)
        {
            MessageBox.Show("ShowLibrary");
        }

        //ShowTopList
        private void ShowTopList(object sender, EventArgs e)
        {
            MessageBox.Show("ShowTopList");
        }
    }

演示者:

   public class MainPresenter : IPresenter
    {
        private IMainView _view;

        // Constructor
        public MainPresenter(IMainView view)
        {
            this._view = view;
            this.WireEvents();
        }

        // Wire events
        public void WireEvents()
        {
            this._view.OnLoad += OnLoad;
            this._view.ShowArtist += ShowArtist;
            this._view.ShowArtist += ShowLibrary;
            this._view.ShowArtist += ShowTopList;
        }

        // On load
        private void OnLoad(object sender, EventArgs e)
        {
            MessageBox.Show("OnLoad");
        }

        // ShowArtist
        private void ShowArtist(object sender, EventArgs e)
        {
            MessageBox.Show("ShowArtist");
        }

        // ShowLibrary
        private void ShowLibrary(object sender, EventArgs e)
        {
            MessageBox.Show("ShowLibrary");
        }

        //ShowTopList
        private void ShowTopList(object sender, EventArgs e)
        {
            MessageBox.Show("ShowTopList");
        }
    }

2 个答案:

答案 0 :(得分:2)

将您的代码更改为以下代码:

// Wire events
public void WireEvents()
{
    this._view.OnLoad += OnLoad;
    this._view.ShowArtist += ShowArtist;
    this._view.ShowLibrary += ShowLibrary;
    this._view.ShowTopList += ShowTopList;
}

这只是一个事件链接错误:)

答案 1 :(得分:1)

您应该正确订阅演示者

中的活动
public void WireEvents()
{
    this._view.OnLoad += OnLoad;
    this._view.ShowArtist += ShowArtist;  // this._view.ShowArtist
    this._view.ShowLibrary += ShowLibrary; // this._view.ShowArtist
    this._view.ShowTopList += ShowTopList; // this._view.ShowArtist
}