我有一个自定义用户控件,在尝试将其添加到设计器视图时触发异常。 (More information on that bug here)
有人告诉我,我必须告诉设计师不承认该控件,因此不会将其序列化。我发现这MSDN article似乎做了我想做的事。我假设这将解决我的错误,希望! :) (如果您有更好的想法,请告诉我如何修复此错误。)
如何将我的课程元数据添加到隐藏或内容?谢谢!
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;
using System.Runtime.Serialization;
using System.ComponentModel.Design.Serialization;
namespace WinformsPlayground
{
[DesignerSerializerAttribute()] //THE QUESTION IS HERE!
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
}
}
编辑:根据这里的建议我将其添加到课程顶部,但编译时收到错误。
namespace WinformsPlayground
{
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public partial class HorizontalPictureScroller : UserControl
{
public HorizontalPictureScroller()
{
InitializeComponent();
Pictures = new ObservableCollection<SelectablePicture>();
Pictures.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Pictures_CollectionChanged);
}
错误是:
错误1属性 'DesignerSerializationVisibility'是 对此声明类型无效。它 仅适用于'方法,财产, 索引器,字段,事件' 声明。 C:\ Users \用户Sergio.Tapia \文档\ Visual 工作室 2010 \ Projects \ WinformsPlayground \ WinformsPlayground \ HorizontalPictureScroller.cs 15 6 WinformsPlayground
答案 0 :(得分:0)
我不确定,但它可能会阻塞您的公共可序列化图片列表。只是为了测试,我会尝试在您的班级
上面的这个属性上方添加以下内容[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
我相信如果你把它隐藏起来,它就不会使它成为属性表中的可编辑值,因此实际上并没有尝试编写对象的序列化内容。
答案 1 :(得分:0)
我尝试了以下内容,似乎对我有用:
internal class HorizontalPictureScrollerSerializer : CodeDomSerializer
{
public override object Deserialize(IDesignerSerializationManager manager, object codeObject)
{
var baseClassSerializer = (CodeDomSerializer) manager.
GetSerializer(
typeof (HorizontalPictureScroller).BaseType,
typeof (CodeDomSerializer));
return baseClassSerializer.Deserialize(manager, codeObject);
}
public override object Serialize(IDesignerSerializationManager manager, object value)
{
var baseClassSerializer = (CodeDomSerializer) manager.GetSerializer(
typeof (HorizontalPictureScroller).BaseType,
typeof (CodeDomSerializer));
object codeObject = baseClassSerializer.Serialize(manager, value);
return codeObject;
}
}
[DesignerSerializerAttribute(typeof (HorizontalPictureScrollerSerializer), typeof (CodeDomSerializer))]
//THE QUESTION IS HERE!
public partial class HorizontalPictureScroller : UserControl
{
.......
}