如何为InitializeComponent提供自定义代码?
当您在设计时修改 ListView 的列标题时,设计器会生成代码以在运行时序列化列标题:
private void InitializeComponent()
{
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2
});
}
表单设计器如何知道它应该调用每列的构造函数,然后调用 AddRange 方法ListView 的 Columns 属性?我需要这个用于像我正在编写的 UserControl 这样的 ListView 。
When you modify column headers of a ListView at design time, the designer generates code to serialize column headers at run-time:
private void InitializeComponent()
{
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2
});
}
How does the forms-designer know that it should call the constructor for each column followed by a call to the AddRange method of the Columns property of the ListView? I need this for a ListView like UserControl I am writing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想要实现的是自定义由我的自定义组件生成的
InitializeComponent
代码。我发现这篇 MSDN 文章描述了如何执行此操作:自定义 .NET 中的代码生成框架可视化设计器
看来我需要编写一个
CodeDomSerializer
为我的组件,并生成CodeExpression
描述了我的自定义初始化代码。What I wanted to achieve was to customize the
InitializeComponent
code produced by my custom component. I found this MSDN article which describes how to do that:Customizing Code Generation in the .NET Framework Visual Designers
It appears that I need to write a
CodeDomSerializer
for my component, and generate a collection ofCodeExpression
's describing my custom initialization code.您可以使用特殊属性来告诉 Visual Studio 设计器如何序列化代码中的属性。有关示例,请参阅 DesignerSerializationVisibilityAttribute 的 MSDN 参考。 本系列文章还很好地概述了可用于扩展自定义控件的设计时支持的各种属性。希望这有帮助。
You can use special attributes to tell the Visual Studio designer how to serialize properties in code. See the MSDN reference for DesignerSerializationVisibilityAttribute for an example. This series of articles also gives a good overview of the various attributes available to extend design time support for custom controls. Hope this helps.