Visual Studio中触发,当我拖我的用户控件拖到设计视图中的错误 [英] Visual Studio fires an error when I drag my user control onto the design view
问题描述
我有两个用户控件,一个是在它一个复选框一个简单的图片持有者。而另一种作用的容器相比具有前一控制的集合。
因此,一个 HorizontalPictureScroller 可以有很多的 SelectablePicture 控制。我会贴小码每个控件:
首先, HorizontalPictureScroller
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Drawing中;
使用System.Data这;
使用System.Linq的;
使用System.Text;使用System.Windows.Forms的
;
使用System.Collections.ObjectModel;
命名空间WinformsPlayground
{
[序列化()]
公共部分类HorizontalPictureScroller:用户控件
{
公共HorizontalPictureScroller()
{
的InitializeComponent();
图片=新的ObservableCollection< SelectablePicture>();
Pictures.CollectionChanged + =新System.Collections.Specialized.NotifyCollectionChangedEventHandler(Pictures_CollectionChanged);
}
#地区的属性
公众的ObservableCollection< SelectablePicture>图片{搞定;组; }
私人INT PositionControlX = 0;
#endregion
#地区的方法
私人无效RedrawPictures()
{
PositionControlX = 0;
的foreach(在图片VAR图片)
{
picture.Location =新的点(PositionControlX + panelPicturesWrapper.AutoScrollPosition.X,0);
PositionControlX + = 130;
panelPicturesWrapper.Controls.Add(图片);
}
}
公共无效给AddPicture(SelectablePicture图片)
{
Pictures.Add(图片);
}
公共无效RemovePicture(SelectablePicture图片)
{
Pictures.Remove(图片);
}
公共无效MovePictureLeft(INT指数)
{
SelectablePicture tmpPicture =图片[指数]
图片[指数] =图片[指数 - 1];
图片[指数 - 1] = tmpPicture;
}
公共无效MovePictureRight(INT指数)
{
SelectablePicture tmpPicture =图片[指数]
图片[指数] =图片[指数+ 1];
图片[指数+ 1] = tmpPicture;
}
#endregion
#地区的事件
无效Pictures_CollectionChanged(对象发件人,System.Collections.Specialized.NotifyCollectionChangedEventArgs E)
{
RedrawPictures();
}
#endregion
}
}
现在,在 SelectablePicture 控件:
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Drawing中;
使用System.Data这;
使用System.Linq的;
使用System.Text;使用System.Windows.Forms的
;
命名空间WinformsPlayground
{
[序列化()]
公共部分类SelectablePicture:用户控件
{
公共SelectablePicture()
{
的InitializeComponent();
panel1.BackgroundImageLayout = ImageLayout.Zoom;
}
公共SelectablePicture(形象画像)
{
panel1.BackgroundImage =图像;
panel1.BackgroundImageLayout = ImageLayout.Zoom;
}
#地区的属性
公众形象形象()
{
返回panel1.BackgroundImage;
}
公共BOOL IsSelected()
{
返回chkSelected.Checked;
}
#endregion
#地区的方法
公共无效ToggleCheckBox()
{
chkSelected.Checked = chkSelected.Checked?假:真实的;
}
公共无效VisuallySelect()
{
this.BackColor = Color.FromArgb(51,153,255);
}
公共无效VisuallyDeselect()
{
//如果没有用户控件内部的控件具有焦点,设置此控制为白色。
如果(this.Focused&安培;!&安培;!this.panel1.Focused和放大器;&安培;!this.chkSelected.Focused)
{
this.BackColor = Color.White;
}
}
#endregion
#地区的事件
私人无效panel1_Click(对象发件人,EventArgs五)
{
VisuallySelect();
ToggleCheckBox();
panel1.Focus();
}
私人无效chkSelected_Click(对象发件人,EventArgs五)
{
VisuallySelect();
ToggleCheckBox();
chkSelected.Focus();
}
私人无效SelectablePicture_Click(对象发件人,EventArgs五)
{
VisuallySelect();
ToggleCheckBox();
this.Focus();
}
私人无效panel1_Leave(对象发件人,EventArgs五)
{
VisuallyDeselect();
}
私人无效chkSelected_Leave(对象发件人,EventArgs五)
{
VisuallyDeselect();
}
私人无效SelectablePicture_Leave(对象发件人,EventArgs五)
{
VisuallyDeselect();
}
#endregion
}
}
这里的错误尝试拖动HorizontalPictureScroller到我的WinForm的设计视图时,我得到的截图(对不起,我不能在这里粘贴文本):
我的用户控件是很简单的,我看不出什么错代码。
也许这是我的一个明显的错误。 :P。非常感谢您的时间
的异常被抛出,因为你正在使用的 SerializableAttribute
,但<$ C 。$ C>用户控件不
从 SerializableAttribute
:如果对象图中的任何类型被序列化没有应用SerializableAttribute属性p>
公共语言运行库将引发SerializationException。
块引用>I have two user controls, one that is a simple picture holder with a checkbox in it. And another which acts a container than has a collection of the previous control.
So a HorizontalPictureScroller can have many SelectablePicture controls. I'll paste the small code for each of the controls:
First, HorizontalPictureScroller:
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 } }
Now, the SelectablePicture control:
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 } }
Here's a screenshot of the error I get when trying to drag the HorizontalPictureScroller onto my Winform design view (Sorry I can't paste the text here):
My user controls are very simple and I can't see what's going wrong in the code.
Maybe it's a blatant mistake on my part. :P Thanks a lot for your time.
解决方案The exception is being thrown because you are using the
SerializableAttribute
, butUserControl
does not.From the documentation for
SerializableAttribute
:The common language runtime throws SerializationException if any type in the graph of objects being serialized does not have the SerializableAttribute attribute applied.
这篇关于Visual Studio中触发,当我拖我的用户控件拖到设计视图中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!