0

有人可以清除我在此代码中的错误...我知道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;


}
4

3 回答 3

2

您使用 ListView 虚拟模式的代码仍然缺少一些重要信息。首先,我们需要为 ListView的VirtualListSize属性设置值。其次,我们需要设置一个缓存值来正确检索 ListItem。

我们应该记住 RetrieveVirtualItem 事件总是需要返回一个 ListViewItem

您可以在这里参考:http: //msdn.microsoft.com/en-us/library/system.windows.forms.listview.virtualmode%28v=vs.90%29.aspx

我已经根据我的假设修改了你的代码,你可以按照你的想法修改它。希望这有帮助。

public partial class Form1 : Form
{
    private ListViewItem[] myCache; //array to cache items for the virtual list 
    private int firstItem; //stores the index of the first item in the cache

    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);
        myCache = new ListViewItem[files.Length];
        int temp = 0;
        foreach (var file in files)
        {

            _fileInfoCollection.Enqueue(new ListViewFileInfo() { FileName = file.Name, FilePath = file.FullName });

            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);
            myCache[temp] = listViewItem;
            temp++;
        }

        listView1.VirtualListSize = myCache.Length;
    }

    private void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
    {
        if (myCache != null && e.ItemIndex >= firstItem && e.ItemIndex < firstItem + myCache.Length)
        {
            //A cache hit, so get the ListViewItem from the cache instead of making a new one.
            e.Item = myCache[e.ItemIndex - firstItem];
        }
        else
        {
            //A cache miss, so create a new ListViewItem and pass it back. 
            e.Item = new ListViewItem();
        }
    }

    private void comboBoxDrive_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetFileInformation(comboBoxDrive.Text);
    }


    private Queue<ListViewFileInfo> _fileInfoCollection;
}
于 2013-04-15T03:54:15.617 回答
1

我遇到过几次这个问题,对于像我一样使用 ObjectListView (FastObjectListView) 开源库的任何人,或者只是使用带有数据源的 Virtual ListView,我发现问题不在于列表或数据本身,它与数据的来源有关。

很难从使用虚拟列表时给出的错误中诊断出来,但是当我将 FastObjectListView 更改为 ObjectListView (基本上删除了虚拟功能)时,我能够看到导致异常的实际错误,并且它是一个视图我的实体框架模型上不再存在曾经存在的东西。

希望这对某人有所帮助,因为它让我挠头并从谷歌上搜索出那个无用的错误消息。

于 2017-03-29T12:04:06.540 回答
0

这是完整的代码..

 /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.listView1 = new System.Windows.Forms.ListView();
        this.columnHeaderIndex = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
        this.columnHeaderInformation = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
        this.comboBoxDrive = new System.Windows.Forms.ComboBox();
        this.SuspendLayout();
        // 
        // listView1
        // 
        this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
        this.columnHeaderIndex,
        this.columnHeaderInformation});
        this.listView1.Dock = System.Windows.Forms.DockStyle.Bottom;
        this.listView1.Location = new System.Drawing.Point(0, 24);
        this.listView1.Name = "listView1";
        this.listView1.Size = new System.Drawing.Size(551, 249);
        this.listView1.TabIndex = 0;
        this.listView1.UseCompatibleStateImageBehavior = false;
        this.listView1.View = System.Windows.Forms.View.Details;
        this.listView1.VirtualListSize = 500;
        this.listView1.VirtualMode = true;
        this.listView1.RetrieveVirtualItem += new System.Windows.Forms.RetrieveVirtualItemEventHandler(this.listView1_RetrieveVirtualItem);
        // 
        // columnHeaderIndex
        // 
        this.columnHeaderIndex.Text = "Index";
        // 
        // columnHeaderInformation
        // 
        this.columnHeaderInformation.Text = "Information";
        this.columnHeaderInformation.Width = 400;
        // 
        // comboBoxDrive
        // 
        this.comboBoxDrive.FormattingEnabled = true;
        this.comboBoxDrive.Items.AddRange(new object[] {
        "C:",
        "D:"});
        this.comboBoxDrive.Location = new System.Drawing.Point(-4, 0);
        this.comboBoxDrive.Name = "comboBoxDrive";
        this.comboBoxDrive.Size = new System.Drawing.Size(121, 21);
        this.comboBoxDrive.TabIndex = 2;
        this.comboBoxDrive.SelectedIndexChanged += new System.EventHandler(this.comboBoxDrive_SelectedIndexChanged);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(551, 273);
        this.Controls.Add(this.comboBoxDrive);
        this.Controls.Add(this.listView1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.ListView listView1;
    private System.Windows.Forms.ColumnHeader columnHeaderInformation;
    private System.Windows.Forms.ColumnHeader columnHeaderIndex;
    private ComboBox comboBoxDrive;

我是否错过了代码中的其他内容。请 !核实。我很好奇看到输出。

于 2013-04-15T14:40:25.367 回答