在一个自定义控件里定义了这样一个类: public class MyItem
    {
        private ImageList imgList = null;
        private int imgIndex = -1;
        private List<MySubItem> subItems = new List<MySubItem>();
        private bool isChecked = true;        /// <summary>
        /// 该项的选中状态
        /// </summary>
        public bool IsChecked
        {
            get { return isChecked; }
            set { isChecked = value; }
        }        /// <summary>
        /// 子项内容
        /// </summary>
        public List<MySubItem> SubItems
        {
            get { return subItems; }
            set { subItems = value; }
        }        /// <summary>
        /// 图标的序号
        /// </summary>
        public int ImgIndex
        {
            get { return imgIndex; }
            set { imgIndex = value; }
        }
        /// <summary>
        /// 该项的图标
        /// </summary>
        public ImageList ImgList
        {
            get { return imgList; }
            set { imgList = value; }
        }
    }
 编辑的时候没有错误,可是一用在窗体是时候就报出: "未标记为可序列化" 的错误~ 为什么呢?不是在WinCE下面是不能序列化的吗,怎么会报出这个错误呢?想打上[Sei...]都不行,WinCE下面不支持啊~

解决方案 »

  1.   


     public class MyItem
        {
            #region Fields
            
            private ImageList imgList = null;
            private int imgIndex = -1;
            private List<MySubItem> subItems = new List<MySubItem>();
            private bool isChecked = true;        #endregion        #region Properties
            
            /// <summary>
            /// 该项的选中状态
            /// </summary>
            public bool IsChecked
            {
                get { return isChecked; }
                set { isChecked = value; }
            }        /// <summary>
            /// 子项内容
            /// </summary>
            public List<MySubItem> SubItems
            {
                get { return subItems; }
                set { subItems = value; }
            }        /// <summary>
            /// 图标的序号
            /// </summary>
            public int ImgIndex
            {
                get { return imgIndex; }
                set { imgIndex = value; }
            }        /// <summary>
            /// 该项的图标
            /// </summary>
            public ImageList ImgList
            {
                get { return imgList; }
                set { imgList = value; }
            }        #endregion        #region Construction
            
            public MyItem()
            {
            }        public MyItem(bool check, List<MySubItem> sbs, int imgin, ImageList imgli)
            {
                this.isChecked = check;
                this.subItems = sbs;
                this.imgIndex = imgin;
                this.imgList = imgli;
            }        public MyItem(MyItem temp)
            {
                this.isChecked = temp.isChecked;
                this.subItems = temp.subItems;
                this.imgIndex = temp.imgIndex;
                this.imgList = temp.imgList;
            }
            #endregion
        }
    加了三个构造函数也还是会出现同样的错误,真不知道为什么
      

  2.   

    那就标记为可序列化的试下,呵呵。反正我是不懂    [Serializable]
        public class MyItem
        {
            #region Fields        private ImageList imgList = null;
            private int imgIndex = -1;
            private List<MySubItem> subItems = new List<MySubItem>();
            private bool isChecked = true;        #endregion        #region Properties        /// <summary>
            /// 该项的选中状态
            /// </summary>
            public bool IsChecked
            {
                get { return isChecked; }
                set { isChecked = value; }
            }        /// <summary>
            /// 子项内容
            /// </summary>
            public List<MySubItem> SubItems
            {
                get { return subItems; }
                set { subItems = value; }
            }        /// <summary>
            /// 图标的序号
            /// </summary>
            public int ImgIndex
            {
                get { return imgIndex; }
                set { imgIndex = value; }
            }        /// <summary>
            /// 该项的图标
            /// </summary>
            public ImageList ImgList
            {
                get { return imgList; }
                set { imgList = value; }
            }        #endregion        #region Construction        public MyItem()
            {
            }        public MyItem(bool check, List<MySubItem> sbs, int imgin, ImageList imgli)
            {
                this.isChecked = check;
                this.subItems = sbs;
                this.imgIndex = imgin;
                this.imgList = imgli;
            }        public MyItem(MyItem temp)
            {
                this.isChecked = temp.isChecked;
                this.subItems = temp.subItems;
                this.imgIndex = temp.imgIndex;
                this.imgList = temp.imgList;
            }        private MyItem(SerializationInfo info, StreamingContext context)
                : base(info, context) 
            {
               ……
               ……
            }        /// <summary>
            /// 重写GetObjectData方法。
            /// </summary>
            /// <param name="info"></param>
            /// <param name="context"></param>
            public override void GetObjectData(SerializationInfo info, StreamingContext context)
            {                        //  调用基类方法,序列化它的成员 
                base.GetObjectData(info, context);
            }        #endregion
        }
      

  3.   

    [Serializable] 标记在CE下面是不支持的,写上去编辑会报错~
      

  4.   

    全部代码如下:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;namespace CE_ListView
    {    
        public partial class CE_ListView : UserControl
        {
            public CE_ListView()
            {
                InitializeComponent();            
            }        #region Fields
            
            private Color columnHeaderColor = Color.FromArgb(255, 255, 192);
            private Color gridColor = Color.Gray;
            private Color borderColor = Color.Black;
            private bool showGrid = true;
            private int rowHeight = 21;
            private int columnHeadHeight = 22;
            private List<MyColumn> columns = new List<MyColumn>();
            private List<MyItem> items = new List<MyItem>();
            private int selectRowIndex = -1;
            private int selectColumnIndex = -1;        #endregion        #region Properties
            
            /// <summary>
            /// 鼠标当前点击的列号
            /// </summary>
            public int SelectColumnIndex
            {
                get { return selectColumnIndex; }
                set { selectColumnIndex = value; }
            }        /// <summary>
            /// 鼠标当前点击的行号
            /// </summary>
            public int SelectRowIndex
            {
                get { return selectRowIndex; }
                set { selectRowIndex = value; }
            }        /// <summary>
            /// 行
            /// </summary>      
         
            public List<MyItem> Items
            {
                get { return items; }
                set { items = value; }
            }
            /// <summary>
            /// 列
            /// </summary>
            public List<MyColumn> Columns
            {
                get { return columns; }
                set { columns = value; }
            }
            /// <summary>
            /// 列头的高
            /// </summary>
            public int ColumnHeadHeight
            {
                get { return columnHeadHeight; }
                set { columnHeadHeight = value; }
            }        /// <summary>
            /// 行高
            /// </summary>
            public int RowHeight
            {
                get { return rowHeight; }
                set { rowHeight = value; }
            }        /// <summary>
            /// 是否显示格网
            /// </summary>
            public bool ShowGrid
            {
                get { return showGrid; }
                set
                {
                    if (showGrid != value)
                    {
                        showGrid = value;
                        this.Invalidate();
                    }
                }
            }        /// <summary>
            /// 边框颜色
            /// </summary>
            public Color BorderColor
            {
                get { return borderColor; }
                set { borderColor = value; }
            }        /// <summary>
            /// 格网颜色
            /// </summary>
            public Color GridColor
            {
                get { return gridColor; }
                set { gridColor = value; }
            }        /// <summary>
            /// 列头颜色
            /// </summary>
            public Color ColumnHeaderColor
            {
                get { return columnHeaderColor; }
                set { columnHeaderColor = value; }
            }        #endregion        #region 重写的事件        protected override void OnPaintBackground(PaintEventArgs e)
            {
                e.Graphics.Clear(BackColor);
                if (showGrid == true)
                    DrawGrid(e.Graphics);
                DrawColumnHeader(e.Graphics);            e.Graphics.DrawRectangle(new Pen(borderColor, 1), new Rectangle(0, 0, this.Width, this.Height));
            }        #endregion        #region Methods        /// <summary>
            /// 画列头
            /// </summary>
            private void DrawColumnHeader(Graphics g)
            {
                g.FillRectangle(new SolidBrush(columnHeaderColor), new Rectangle(0, 0, this.Width, columnHeadHeight));
                SizeF textSize = new SizeF();
                System.Drawing.Font font = new Font("宋体", 10, FontStyle.Regular);
                using (Pen gridPen = new Pen(Color.Gray, 1))
                {
                    for (int col = 0; col < columns.Count; col++)
                    {
                        int width = GetColumnsWidth(col + 1);
                        g.DrawLine(gridPen, width, 0, width, 22);
                        textSize = g.MeasureString(columns[col].Title, font);                    Image img = null;
                        int imgWidth = 0;                    if (columns[col].ImgList != null)
                        {
                            img = columns[col].ImgList.Images[columns[col].ImageIndex];
                            imgWidth = img.Width;
                        }
                        int x = (int)(((width - GetColumnsWidth(col)) / 2 - (textSize.Width + imgWidth) / 2) + GetColumnsWidth(col));
                        int y = (int)((11 - textSize.Height / 2) + 2);
                        g.DrawString(columns[col].Title, Font, new SolidBrush(ForeColor), new RectangleF(x + imgWidth + 2, y, textSize.Width + 10, textSize.Height));
                        if (columns[col].ImgList != null && columns[col].ImageIndex >= 0 && columns[col].ImageIndex < columns[col].ImgList.Images.Count)
                        {
                            g.DrawImage(columns[col].ImgList.Images[columns[col].ImageIndex], x, y);
                        }
                    }
                    g.DrawRectangle(new Pen(Color.Black, 1), new Rectangle(0, 0, this.Width, 22));
                }
            }        /// <summary>
            /// 画格网
            /// </summary>
            /// <param name="g"></param>
            private void DrawGrid(Graphics g)
            {
                using (Pen gridPen = new Pen(gridColor, 1))
                {
                    for (int row = 0; row < this.Height; row++)
                    {
                        g.DrawLine(gridPen, 0, 22 + row * (rowHeight), this.Width, 22 + row * (rowHeight));
                    }
                    for (int col = 0; col < columns.Count + 1; col++)
                    {
                        int width = GetColumnsWidth(col);
                        g.DrawLine(gridPen, width, 22, width, this.Height);
                    }
                }
            }        private void DrawItem(Graphics g)
            {
            }        /// <summary>
            /// 计算当前所在列的总宽度
            /// </summary>
            /// <param name="columnIndex"></param>
            /// <returns></returns>
            private int GetColumnsWidth(int columnIndex)
            {
                int width = 0;
                for (int i = 0; i < columnIndex; i++)
                {
                    width += columns[i].Width;
                }
                return width;
            }        /// <summary>
            /// 判断当前鼠标点击哪一行哪一列
            /// </summary>
            /// <param name="p"></param>
            private void GetRow_ColumnIndex(Point p)
            {
                if (columns.Count < 1)
                    return;
                if (items.Count < 1)
                    return;
                if (p.Y <= 22)
                    selectRowIndex = -1;
                else
                    selectRowIndex = (p.Y - 22) / rowHeight;
                if (p.X < columns[0].Width)
                    selectColumnIndex = 0;
                else if (p.X > GetColumnsWidth(columns.Count))
                    selectColumnIndex = columns.Count;
                else
                {
                    int pastWidth = 0;
                    int nextWidth = 0;
                    for (int i = 1; i < columns.Count; i++)
                    {
                        pastWidth = GetColumnsWidth(i);
                        nextWidth = GetColumnsWidth(i + 1);
                        if (p.X > pastWidth && p.Y < nextWidth)
                        {
                            selectColumnIndex = i;
                            break;
                        }
                    }
                }
            }        #endregion
        }接下:
      

  5.   

    接11楼:    public enum ColumnStyle : byte
        {
            Label = 1,
            CheckBox = 2
        }    public class MyColumn
        {
            #region Construction        public MyColumn()
            {
            }        public MyColumn(ColumnStyle cs, bool resize, int w, ImageList imgli, int imgin, string text)
            {
                this.style = cs;
                this.resizable = resize;
                this.width = w;
                this.imgList = imgli;
                this.imgaeIndex = imgin;
                this.title = text;
            }        public MyColumn(MyColumn temp)
            {
                this.style = temp.style;
                this.resizable = temp.resizable;
                this.width = temp.width;
                this.imgList = temp.imgList;
                this.imgaeIndex = temp.imgaeIndex;
                this.title = temp.title;
            }        #endregion        #region Fields        private string title = "";
            private int imgaeIndex = -1;
            private ImageList imgList = null;
            private int width = 100;
            private bool resizable = false;
            private ColumnStyle style = ColumnStyle.Label;        #endregion        #region Properties        /// <summary>
            /// 控件样式
            /// </summary>
            public ColumnStyle Style
            {
                get { return style; }
                set
                {
                    style = value;
                    if (style == ColumnStyle.CheckBox)
                    {
                        resizable = false;
                        width = 22;
                    }
                }
            }
            /// <summary>
            /// 可否改变列宽
            /// </summary>
            public bool Resizable
            {
                get { return resizable; }
                set
                {
                    if (style == ColumnStyle.CheckBox)
                        return;
                    resizable = value;
                }
            }
            /// <summary>
            /// 列宽
            /// </summary>
            public int Width
            {
                get { return width; }
                set
                {
                    if (style == ColumnStyle.CheckBox)
                        return;
                    width = value;
                }
            }        /// <summary>
            /// 图像列表容器
            /// </summary>
            public ImageList ImgList
            {
                get { return imgList; }
                set { imgList = value; }
            }
            /// <summary>
            /// 图像列表
            /// </summary>
            public int ImageIndex
            {
                get { return imgaeIndex; }
                set { imgaeIndex = value; }
            }
            /// <summary>
            /// 列头标题
            /// </summary>
            public string Title
            {
                get { return title; }
                set { title = value; }
            }        #endregion
        }
        public class MySubItem
        {
            #region Constrution        public MySubItem()
            {
            }        public MySubItem(string t, bool check)
            {
                this.text = t;
                this.isChecked = check;
            }        public MySubItem(MySubItem temp)
            {
                this.text = temp.text;
                this.isChecked = temp.isChecked;
            }        #endregion        #region Fields        private string text = "";
            private bool isChecked = true;        #endregion        #region Properties        /// <summary>
            /// 子项的选中状态
            /// </summary>
            public bool IsChecked
            {
                get { return isChecked; }
                set { isChecked = value; }
            }        /// <summary>
            /// 子项的显示内容
            /// </summary>
            public string Text
            {
                get { return text; }
                set { text = value; }
            }        #endregion
        }    public class MyItem
        {
            #region Construction        public MyItem()
            {
            }        public MyItem(bool check, List<MySubItem> sbs, int imgin, ImageList imgli)
            {
                this.isChecked = check;
                this.subItems = sbs;
                this.imgIndex = imgin;
                this.imgList = imgli;
            }        public MyItem(MyItem temp)
            {
                this.isChecked = temp.isChecked;
                this.subItems = temp.subItems;
                this.imgIndex = temp.imgIndex;
                this.imgList = temp.imgList;
            }
            #endregion        #region Fields        private ImageList imgList = null;
            private int imgIndex = -1;
            
            private List<MySubItem> subItems = new List<MySubItem>();
            private bool isChecked = true;        #endregion        #region Properties        /// <summary>
            /// 该项的选中状态
            /// </summary>
            public bool IsChecked
            {
                get { return isChecked; }
                set { isChecked = value; }
            }        /// <summary>
            /// 子项内容
            /// </summary>
            public List<MySubItem> SubItems
            {
                get { return subItems; }
                set { subItems = value; }
            }        /// <summary>
            /// 图标的序号
            /// </summary>
            public int ImgIndex
            {
                get { return imgIndex; }
                set { imgIndex = value; }
            }        /// <summary>
            /// 该项的图标
            /// </summary>
            public ImageList ImgList
            {
                get { return imgList; }
                set { imgList = value; }
            }        #endregion
        }
    }
    编辑的时候是没问题的,就是将控件拖到窗体的时候就出现了如下错误:创建组件"CE_ListView"失败.错误消息为:
     "System.Runtime.Serialization.SerialzationException: 程序集:
    "CE_ListView, Version=1.0.3085.18713,Culture=netural,
    PublicKeyToken=null" 中的类型"CE_ListView.MyItem"未标记为可序列化.
    在.....