清除 ListView 项会在使用事件时创建重复项

Clearing ListView items create duplicate items when using events

我有一个列表视图,我希望能够从项目中清除它,然后再次填充它。
我需要将它与我的 Engine class.

事件一起使用

当我清除项目并再次 运行 时,问题就开始了,它重复了我的项目:

知道为什么会发生以及如何解决吗?
清除列表视图或初始化新列表视图对我不起作用。

我的表单代码:

using System;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private delegate void createListViewCallBack(string i_File, string i_FileStatus);
        private void createListView(string i_File, string i_FileStatus)
        {

            if (this.InvokeRequired)
            {
                createListViewCallBack s = new createListViewCallBack(createListView);
                this.Invoke(s, i_File, i_FileStatus);
            }
            else
            {
                ListViewItem item = new ListViewItem(i_File);
                item.SubItems.Add(i_FileStatus);
                listView1.Items.Add(item);
            }
        }


        private void buttonStart_Click(object sender, EventArgs e)
        {
            Engine.BuildListViewUpdate += Engine_BuildListViewUpdate;
            Engine.BuildList();
            //createListView(@"c:\a.txt", @"yes");
            //createListView(@"c:\b.txt", @"no");
        }


        private delegate void buildListUpdateCallBack(string i_File, string i_FileStatus);
        private void Engine_BuildListViewUpdate(string i_File, string i_FileStatus)
        {
            if (this.InvokeRequired)
            {
                buildListUpdateCallBack s = new buildListUpdateCallBack(Engine_BuildListViewUpdate);
                this.Invoke(s, i_File, i_FileStatus);
            }
            else
            {
                ListViewItem item = new ListViewItem(i_File);
                item.SubItems.Add(i_FileStatus);
                listView1.Items.Add(item);
            }
        }

        private void clear1()
        {
            this.listView1 = new ListView();
            //this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            //this.columnHeader1,
            //this.columnHeader2});
            this.listView1.HideSelection = false;
            this.listView1.Location = new System.Drawing.Point(55, 156);
            this.listView1.Name = "listView1";
            this.listView1.Size = new System.Drawing.Size(633, 265);
            this.listView1.TabIndex = 1;
            this.listView1.UseCompatibleStateImageBehavior = false;
            this.listView1.View = System.Windows.Forms.View.Details;
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            //listView1.Clear();
            //clear1();
        }
    }
}


我的引擎class:


namespace WindowsFormsApp3
{
    public delegate void BuildListViewEventHandler(string i_File, string i_FileStatus);
    static class Engine
    {
        public static event BuildListViewEventHandler BuildListViewUpdate;

        public static void BuildList()
        {
            BuildListViewUpdate(@"c:\a.txt", @"yes");
            BuildListViewUpdate(@"c:\b.txt", @"no");
        }

        public static void OnBuildListViewUpdate(string i_File, string i_FileStatus)
        {
            if (BuildListViewUpdate != null)
            {
                BuildListViewUpdate.Invoke(i_File, i_FileStatus);
            }
        }
    }
}


问题是您每次单击开始时都在订阅该事件。

因此,当您第二次单击开始按钮时,该事件将触发两次,依此类推。

要解决此问题,请在程序开始时仅订阅一次事件或在清除列表时取消订阅

private void buttonClear_Click(object sender, EventArgs e)
{
    listView1.Items.Clear();
    Engine.BuildListViewUpdate -= Engine_BuildListViewUpdate;
}