Goz*_*Goz 10 c# collections user-controls properties serializable
我必须遗漏一些非常明显的东西.我是C#的新手,但已经用C/C++编程多年了,很抱歉,如果这是一个非常明显的东西;)
[请参阅编辑以获取新问题]
我正在尝试创建一个包含UserControl的节点.我有控件出现在WinForm设计器中,我可以添加节点.但是,当我尝试运行代码时,我收到以下错误:
属性"节点"的代码生成失败.错误是:程序集'应用程序中的'Type App.Node',版本= 1.0.0.0,Culture = neutral,PublicKeyToken = null'未标记为可序列化.
然后我添加的节点都没有显示出来.
这开始让我发疯,据我所知,它被标记为可序列化.
该节点定义如下:
[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; }
};
Run Code Online (Sandbox Code Playgroud)
然后我定义一个"集合"如下:
[Serializable]
public class NodeCollection : List< Node >
{
public NodeCollection() :
base()
{
}
};
Run Code Online (Sandbox Code Playgroud)
正如您所见,集合和节点本身都具有"Serializable"属性集.
错误中提到的Nodes属性定义如下
private NodeCollection mNodes = new NodeCollection();
[Category( "Behavior" )]
[Description( "Nodes" )]
public NodeCollection Nodes
{
get
{
return mNodes;
}
}
Run Code Online (Sandbox Code Playgroud)
所以有谁知道我在这里做错了什么?
编辑:回应Archeg的评论,这是我的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; }
}
Run Code Online (Sandbox Code Playgroud)
我做了一些修改,因为我最初发布的一般有关"DesignerSerializationVisibility"属性已经帮助但我现在得到以下构建错误:
错误MSB3103:无效的Resx文件.无法加载类型App.Node,App,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null,它在.RESX文件中使用.确保已将必要的引用添加到项目中.
编辑2:值得注意的是,我的问题只发生在我在设计器中添加一堆节点然后我得到上面的Resx错误.如果我从代码手动添加节点,那么这一切都按照我的预期运行...
Rza*_*sar 22
我相信您遇到此问题,因为Designer会自动尝试序列化所有公共UserControl属性.如果您的自定义UserControl设计时支持不需要此属性,则可以添加"DesignerSerializationVisibility"属性:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
Run Code Online (Sandbox Code Playgroud)
或者只是省略属性get{}和set{}方法,并将其用作公共字段.
希望能帮助到你!
| 归档时间: |
|
| 查看次数: |
14117 次 |
| 最近记录: |