收集用户控件没有标记为可序列化 [英] UserControl collection not marked as serializable

查看:1336
本文介绍了收集用户控件没有标记为可序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须失去了一些东西真的很明显。我很新的C#,但在C / C ++已编程多年,很抱歉,如果选择的是一清二楚;)

I must be missing something really obvious. I'm quite new to C# but have been programming in C/C++ for years, so sorry if it IS something blindingly obvious ;)

[见编辑的新问题]

我试图创建一个包含用户控件的一个节点。我有控制WinForm设计出现,我可以添加节点到它。然而,当我尝试运行代码我得到以下错误:

I'm trying to create a node containing UserControl. I have the Control appearing in the WinForm designer and I can add nodes to it. However when I try and run the code I get the following error:

代码生成属性节点失败。错误是:在大会应用,版本1.0.0.0 =文化=中立,
公钥=空'未标记为可序列

Code generation for property 'Nodes' failed. Error was: 'Type App.Node' in Assembly 'App, version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

再没有节点我加露面。

这是开始我逼疯的, 。据我所看到的,它被标记为可序列化

This is beginning to drive me mad as, as far as I can see, it IS marked as serializable.

节点定义如下:

[Serializable]
public class Node : MarshalByRefObject
{
    public Node()
    {
    }

    public Node( String text )
    {
        this.Text       = text;
        this.Checked    = false;
        this.Complete   = false;
    }

    public String       Text        { get; set; }
    public bool         Checked     { get; set; }
    public bool         Complete    { get; set; }
    public bool         Selected    { get; set; }
};



然后我定义了一个宝典如下:

I then define a "Collection" as follows:

[Serializable]
public class NodeCollection : List< Node >
{
    public NodeCollection() :
        base()
    {
    }
};



无论是收集和节点本身有序列化属性设置为你所看到的。

Both the collection and the node itself have the "Serializable" attribute set as you can see.

在错误中提到的节点属性定义如下:

The Nodes property mentioned in the error is defined as follows

    private NodeCollection      mNodes  = new NodeCollection();

    [Category( "Behavior" )]
    [Description( "Nodes" )]
    public NodeCollection Nodes
    { 
        get
        {
            return mNodes;
        }
    }



所以,有没有人有任何想法什么我做?错在这里。

So has anyone got any idea whats I'm doing wrong here?

编辑:在回应Archeg的意见,这是我的用户:

In response to Archeg's comments this is my UserControl:

public partial class Control : UserControl
{
    public Control()
    {
        InitializeComponent();
    }

    protected override void OnPaint( PaintEventArgs pe )
    {
        Graphics graph  = pe.Graphics;

        int rowHeight   = Font.Height + 2;

        if ( Nodes != null )
        {
            int yPos    = 0;
            foreach( Node node in this.Nodes )
            {
                // Calculate my various bounding boxes.
                Rectangle nodeBounds    = new Rectangle( Bounds.Left, yPos, Bounds.Width, rowHeight );
                Rectangle lightBounds   = new Rectangle( Bounds.Right - Font.Height, yPos, rowHeight, rowHeight );
                Rectangle spannerBounds = new Rectangle( lightBounds.Left - Font.Height, yPos, rowHeight, rowHeight );
                Rectangle checkBoxBound = new Rectangle( 32, yPos, rowHeight, rowHeight );
                Rectangle textBounds    = new Rectangle( checkBoxBound.Right, yPos, Bounds.Width - (rowHeight * 2) - checkBoxBound.Right, rowHeight );

                // Draw selection box.
                Brush textColour    = Brushes.Black;
                if ( node.Selected )
                {
                    graph.FillRectangle( Brushes.Blue, nodeBounds );
                    textColour      = Brushes.Yellow;
                }

                // Draw node text.
                graph.DrawString( node.Text, Font, textColour, textBounds );

                // Draw Red/Green light
                Image[] lightImages = new Image[] { CompleteLightImage, InCompleteLightImage };
                Image lightImage    = lightImages[node.Complete ? 1 : 0];
                if ( lightImage != null )
                {
                    graph.DrawImage( lightImage, lightBounds );
                }

                // Draw Spanner Icon
                if ( SettingsImage != null )
                {
                    graph.DrawImage( SettingsImage, spannerBounds );
                }
                // Draw check box.
                VisualStyleRenderer renderer    = null;
                VisualStyleElement  ve          = node.Checked ? VisualStyleElement.Button.CheckBox.CheckedPressed : VisualStyleElement.Button.CheckBox.CheckedNormal;
                if (VisualStyleRenderer.IsElementDefined( ve ))
                {
                    renderer = new VisualStyleRenderer( ve );
                }

                if ( renderer != null )
                {
                    renderer.DrawBackground( graph, checkBoxBound );
                }
                else
                {
                    ControlPaint.DrawCheckBox( graph, checkBoxBound, node.Checked ? ButtonState.Checked : ButtonState.Normal );
                }
                yPos    += Font.Height;
            }
        }
    }

    private NodeCollection      mNodes  = new NodeCollection();

    [Category( "Behavior" )]
    [Description( "Nodes" )]
    [DesignerSerializationVisibility( DesignerSerializationVisibility.Content )]
    [MergableProperty( false )]
    [Bindable( false )]
    public NodeCollection Nodes
    { 
        get
        {
            return mNodes;
        }
    }

    public Image CompleteLightImage         { get; set; }
    public Image InCompleteLightImage       { get; set; }
    public Image SettingsImage              { get; set; }
}



我已经做了一些修改,因为我原来一般发布有关DesignerSerializationVisibility 属性,这有助于但现在我得到以下生成错误:

I have made some modifications since I originally posted generally relating to the "DesignerSerializationVisibility" attribute which has helped but I am now getting the following build error:

错误MSB3103:无效的ResX文件。未能加载类型App.Node,应用程序,
版= 1.0.0.0,文化=中立,公钥=空这是在
.resx文件中。 。确保必要的引用已经被添加
到项目

error MSB3103: Invalid Resx file. Could not load type App.Node, App, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null which is used in the .RESX file. Ensure that the necessary references have been added to your project.

编辑2 :其值得注意的是,当我在设计中添加了一堆的节点我的问题只发生然后我得到上述RESX错误。如果我从代码中手动添加节点那么它所有的作品,我会期望...

Edit 2: Its worth noting that my problems only occur when I add a bunch of Nodes in the designer then I get the above Resx error. If I add the nodes manually from code then it all works as I'd expect ...

推荐答案

我相信你有这个问题,因为设计器会自动尝试序列的所有公共用户控件的属性。如果不需要为您的自定义用户控件的设计时支持这个属性,那么你可以加入DesignerSerializationVisibility属性:

I believe that you have this problem because Designer automatically tries to serialize all public UserControl properties. If this property is not needed for your custom UserControl design time support, then you can Add "DesignerSerializationVisibility" attribute:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] 

或干脆省略获得{} 属性的集合{} 方法和使用它作为一个公共领域。

or simply omit the get{} and set{} methods of the property and use it as a public field.

希望它帮助!

这篇关于收集用户控件没有标记为可序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆