简体   繁体   English

如何在自定义用户控件上使用DesignerSerializerAttribute?

[英]How to use DesignerSerializerAttribute on a custom User Control?

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. 我发现这篇MSDN文章似乎可以满足我的要求。 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. 错误1属性'DesignerSerializationVisibility'在此声明类型上无效。 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 C:\\ Users \\ Sergio.Tapia \\ documents \\ visual studio 2010 \\ Projects \\ WinformsPlayground \\ WinformsPlayground \\ Horizo​​ntalPictureScroller.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)] [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
    {
                         .......
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM