有人可以清除我在此代码中的错误...我知道listView1_RetrieveVirtualItem
方法存在问题,但我无法纠正。我收到此错误:
ListView 虚拟化需要 RetrieveVirtualItem 事件或 OnRetrieveVirtualItem 方法提供有效的 ListViewItem。
这是我的代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
_fileInfoCollection = new Queue<ListViewFileInfo>();
}
private void GetFileInformation(string drive)
{
_fileInfoCollection.Clear();
var directory = new DirectoryInfo(drive);
var files = directory.GetFiles("*.*", SearchOption.TopDirectoryOnly);
foreach (var file in files)
{
_fileInfoCollection.Enqueue(new ListViewFileInfo() { FileName = file.Name, FilePath = file.FullName });
}
}
private void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
if (_fileInfoCollection.Count > 0)
{
ListViewFileInfo fileInfo = _fileInfoCollection.Dequeue();
var listViewItem = new ListViewItem();
listViewItem.Text = fileInfo.FileName;
var listViewSubItem = new ListViewItem.ListViewSubItem();
listViewSubItem.Text = fileInfo.FilePath;
listViewItem.SubItems.Add(listViewSubItem);
e.Item = listViewItem;
}
}
private void comboBoxDrive_SelectedIndexChanged(object sender, EventArgs e)
{
GetFileInformation(comboBoxDrive.Text);
}
private Queue<ListViewFileInfo> _fileInfoCollection;
}