c# - 当我将我的用户控件拖到设计 View 上时,Visual Studio 引发错误

标签 c# winforms user-controls

我有两个用户控件,一个是带有复选框的简单图片夹。另一个充当容器而不是具有先前控件的集合。

所以一个 Horizo​​ntalPictureScroller 可以有很多 SelectablePicture 控件。我将为每个控件粘贴小代码:

首先,Horizo​​ntalPictureScroller:

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

namespace WinformsPlayground
{
    [Serializable()]
    public partial class HorizontalPictureScroller : UserControl
    {
        public HorizontalPictureScroller()
        {
            InitializeComponent();
            Pictures = new ObservableCollection<SelectablePicture>();
            Pictures.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Pictures_CollectionChanged);
        }       

        #region "Properties"
        public ObservableCollection<SelectablePicture> Pictures { get; set; }
        private int PositionControlX = 0;
        #endregion

        #region "Methods"
        private void RedrawPictures()
        {
            PositionControlX = 0;

            foreach (var picture in Pictures)
            {
                picture.Location = new Point(PositionControlX + panelPicturesWrapper.AutoScrollPosition.X, 0);
                PositionControlX += 130;
                panelPicturesWrapper.Controls.Add(picture);
            }
        }

        public void AddPicture(SelectablePicture picture)
        {
            Pictures.Add(picture);
        }

        public void RemovePicture(SelectablePicture picture)
        {
            Pictures.Remove(picture);
        }

        public void MovePictureLeft(int index)
        {
            SelectablePicture tmpPicture = Pictures[index];
            Pictures[index] = Pictures[index - 1];
            Pictures[index - 1] = tmpPicture;
        }

        public void MovePictureRight(int index)
        {
            SelectablePicture tmpPicture = Pictures[index];
            Pictures[index] = Pictures[index + 1];
            Pictures[index + 1] = tmpPicture;
        }
        #endregion

        #region "Events"
        void Pictures_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            RedrawPictures();
        }        
        #endregion
    }
}

现在,SelectablePicture 控件:

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

namespace WinformsPlayground
{
    [Serializable()]
    public partial class SelectablePicture : UserControl
    {
        public SelectablePicture()
        {
            InitializeComponent();
            panel1.BackgroundImageLayout = ImageLayout.Zoom;
        }

        public SelectablePicture(Image image)
        {
            panel1.BackgroundImage = image;
            panel1.BackgroundImageLayout = ImageLayout.Zoom;
        }

        #region "Properties"
        public Image Image()
        {
            return panel1.BackgroundImage;
        }

        public bool IsSelected()
        {
            return chkSelected.Checked;
        }
        #endregion

        #region "Methods"
        public void ToggleCheckBox()
        {
            chkSelected.Checked = chkSelected.Checked ? false : true;
        }

        public void VisuallySelect()
        {
            this.BackColor = Color.FromArgb(51, 153, 255);
        }

        public void VisuallyDeselect()
        {
            //If none of the controls inside the usercontrol have focus, set this control to white.
            if (!this.Focused && !this.panel1.Focused && !this.chkSelected.Focused)
            {
                this.BackColor = Color.White;
            }
        }        
        #endregion

        #region "Events"
        private void panel1_Click(object sender, EventArgs e)
        {
            VisuallySelect();
            ToggleCheckBox();
            panel1.Focus();
        }

        private void chkSelected_Click(object sender, EventArgs e)
        {
            VisuallySelect();
            ToggleCheckBox();
            chkSelected.Focus();
        }

        private void SelectablePicture_Click(object sender, EventArgs e)
        {
            VisuallySelect();
            ToggleCheckBox();
            this.Focus();
        }

        private void panel1_Leave(object sender, EventArgs e)
        {
            VisuallyDeselect();
        }

        private void chkSelected_Leave(object sender, EventArgs e)
        {
            VisuallyDeselect();
        }

        private void SelectablePicture_Leave(object sender, EventArgs e)
        {
            VisuallyDeselect();
        }
        #endregion        
    }
}

这是我在尝试将 Horizo​​ntalPictureScroller 拖到我的 Winform 设计 View 时遇到的错误的屏幕截图(抱歉,我无法在此处粘贴文本): alt text

我的用户控件非常简单,我看不出代码中出了什么问题。

也许这是我的一个明显错误。 :P 非常感谢您的宝贵时间。

最佳答案

抛出异常是因为您正在使用 SerializableAttribute , 但 UserControl 没有。

来自 SerializableAttribute 的文档:

The common language runtime throws SerializationException if any type in the graph of objects being serialized does not have the SerializableAttribute attribute applied.

关于c# - 当我将我的用户控件拖到设计 View 上时,Visual Studio 引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4304276/

相关文章:

c# - 在 C# (.NET 4.6) 中关闭 keep-alive FTP 或 FTPS 连接的最佳方法?

c# - 将 MYSQL 数据库中的数据显示到控件,尤其是在 DropDown 的 Combobox 上

c# - foreach 和集合的使用速度慢吗?

c# - 动态处理 UserControl 事件

WPF 设置子窗口标题

c# - 在 C# 中,如何对 "largest"值位于列表中间的列表中的项目进行排序

C# Blazor 5.0 onscroll 事件未触发

C# RSS 聚合,如何以编程方式格式化文本

c# - 在 Windows 窗体开发中更改 C# 用户控件布局

c# - 如何在跳过某些值的同时遍历枚举类型?