我正在尝试创建一个ListViewExtensions类但遇到一些错误

时间:2016-05-03 18:28:58

标签: c# .net winforms

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MinimizeCapture
{
    public partial class ListViewCostumControl : UserControl
    {
        public static ListViewControl lvnf;

        public ListViewCostumControl()
        {
            InitializeComponent();

            lvnf = new ListViewControl();
            lvnf.Location = new Point(50, 50);
            lvnf.Size = new Size(50, 50);
            lvnf.View = View.SmallIcon;
            lvnf.Dock = DockStyle.Fill;
            lvnf.SuspendLayout();
            lvnf.LabelEdit = true;
            lvnf.Sorting = SortOrder.None;
            this.Controls.Add(lvnf);
            lvnf.ResumeLayout(false);
        }

        public class ListViewControl : System.Windows.Forms.ListView
        {
            public ListViewControl()
            {
                this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
                this.SetStyle(ControlStyles.EnableNotifyMessage, true);
            }

            protected override void OnNotifyMessage(System.Windows.Forms.Message m)
            {
                if (m.Msg != 0x14)
                {
                    base.OnNotifyMessage(m);
                }
            }
        }

        public static class ListViewExtensions
        {
            public static ListViewItemCollection AddRange(this ListViewItemCollection source, WindowSnapCollection windows)
            {
                //Create a ListViewItem for each object and set the 
                //various properties appropriately
                source.AddRange(from w in windows
                                select new ListViewItem(w.ToString())
                                {
                                    Tag = w
                                });

                return source;
            }

            public static WindowSnap GetWindowSnap(this ListViewItem source)
            {
                return source.Tag as WindowSnap;
            }

            public static WindowSnap GetSelectedWindowSnap(this ListView source)
   {
       return source.SelectedItem?.GetWindowSnap();
   }

            //Add more methods as needed
        }

        private void ListViewNFTest_Load(object sender, EventArgs e)
        {

        }
    }
}

错误在行

public static ListViewItemCollection AddRange(this ListViewItemCollection source, WindowSnapCollection windows)

错误16找不到类型或命名空间名称“ListViewItemCollection”(您是否缺少using指令或程序集引用?)

return source.SelectedItem?.GetWindowSnap();

错误18'System.Windows.Forms.ListView'不包含'SelectedItem'的定义,并且没有可以找到接受类型'System.Windows.Forms.ListView'的第一个参数的扩展方法'SelectedItem'(是你错过了使用指令或程序集引用?)

错误19方法'GetWindowSnap'没有重载需要0个参数

1 个答案:

答案 0 :(得分:0)

看起来您需要完全限定某些类名。

类ListViewItemCollection在ListView中定义。因此,您需要使用ListView.ListViewItemCollection完全限定它。

$.ajax({
    url: "someUrl",
    type: "POST",
    dataType: "json",
    data: data,
    success: function(dataReturned) {
       // Do something with dataReturned
    }
});

WindowSnap和WindowSnap集合不是.NET的一部分,并且您不提供定义,因此不清楚如何使用它们。弄清楚它们的定义位置并使用完整的类名,包括命名空间。将所有出现的WindowSnap替换为public static ListView.ListViewItemCollection AddRange( this ListView.ListViewItemCollection source, WindowSnapCollection windows) ,WindowSnapCollection也是如此。

有关详细信息,请参阅this MSDN article完全限定名称。