我一定是漏掉了某些非常显然的东西。我对C#相当新,但已经在C/C++中进行编程多年了,如果确实有什么非常明显的问题,请原谅 ;)
[请查看编辑以获取更新的问题]
我正在尝试创建包含UserControl的节点。我在WinForm设计器中看到控件出现,并且我可以向其中添加节点。但是当我尝试运行代码时,我会遇到以下错误:
属性'Nodes'的代码生成失败。 错误是:程序集“App,version=1.0.0.0,Culture=neutral,PublicKeyToken=null”中的类型 App.Node未标记为可序列化。
然后我添加的所有节点都不会显示。
就我所知,它已标记为可序列化,这让我开始发疯了。
该节点定义如下:
[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; }
};
我接下来定义一个“Collection”:
[Serializable]
public class NodeCollection : List< Node >
{
public NodeCollection() :
base()
{
}
};
你可以看到,这个集合和节点本身都设置了"Serializable"属性。
在错误中提到的Nodes属性定义如下:
private NodeCollection mNodes = new NodeCollection();
[Category( "Behavior" )]
[Description( "Nodes" )]
public NodeCollection Nodes
{
get
{
return mNodes;
}
}
有人知道我在这里做错了什么吗?
编辑: 根据 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; }
}
我发布后进行了一些修改,主要涉及“DesignerSerializationVisibility”属性,这有所帮助,但现在出现以下构建错误:
error MSB3103: 无效的 Resx 文件。无法加载在 .RESX 文件中使用的类型 App.Node,App,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null。请确保已向项目添加必要的引用。
编辑2:值得注意的是,只有在我从设计器中添加一堆节点时才会出现上述Resx错误。如果我手动从代码中添加节点,则一切都按照我的期望工作...