gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:25:10 25 4
gpt4 key购买 nike

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

所以一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com