I have a custom user control that fires an exception when trying to add it to the designer view. (More information on that bug here)
I was told that I have to tell the designer to not acknowledge that control so it does not serialize it. I found this MSDN article that seems to do what I want. I'm assuming this will fix my error, hopefully! :) (If you have better ideas please let me know how to fix this bug.)
How can I add that metadata to my class to Hidden or Content? Thanks!
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
}
}
EDIT: Following advice here I added this to the top of the class but I receive an error when compiling.
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);
}
Error is:
Error 1 Attribute 'DesignerSerializationVisibility' is not valid on this declaration type. It is only valid on 'method, property开发者_开发技巧, indexer, field, event' declarations. C:\Users\Sergio.Tapia\documents\visual studio 2010\Projects\WinformsPlayground\WinformsPlayground\HorizontalPictureScroller.cs 15 6 WinformsPlayground
I'm not sure, but it may be choking on your public serializable list of pictures. Just for testing, I would try adding the following immediately above this one property on your class
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
I believe if you make it hidden, it won't make it an editable value from the properties sheet, and thus not actually try to write the object's serialization content.
I've tried the following and seems to work OK for me:
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
{
.......
}
精彩评论