times New Roman'; mso-hansi-font-family: 'times New Roman'">说明:本文翻译自msdn网站上的《Debugging design-time Controls》一文。文章的原作者是Steve Lasker。
times New Roman'; mso-hansi-font-family: 'times New Roman'">
times New Roman'; mso-hansi-font-family: 'times New Roman'">摘要:times New Roman'; mso-hansi-font-family: 'times New Roman'">逐步介绍了如何在VS.NET designertimes New Roman'; mso-hansi-font-family: 'times New Roman'">环境下调试design-timetimes New Roman'; mso-hansi-font-family: 'times New Roman'">的自定义控件,同时描述了在使用设计界面操作控件时可能发生的一系列事件。
times New Roman'; mso-hansi-font-family: 'times New Roman'">介绍
times New Roman'; mso-hansi-font-family: 'times New Roman'">在自定义控件开发过程中的一个重要部分,是检查和调试代码的能力。当在VS.NETtimes New Roman'; mso-hansi-font-family: 'times New Roman'">的design-timetimes New Roman'; mso-hansi-font-family: 'times New Roman'">环境中使用控件或者Windows Formtimes New Roman'; mso-hansi-font-family: 'times New Roman'">时,即使在你的项目并没有运行(runtimes New Roman'; mso-hansi-font-family: 'times New Roman'">)的情况下,你的代码仍能够被执行(executetimes New Roman'; mso-hansi-font-family: 'times New Roman'">)。这一点使得如何调试代码成为一个挑战。如果你曾经创建过任何会在design-timetimes New Roman'; mso-hansi-font-family: 'times New Roman'">用到的对象,那么这篇文章将告诉你,当你在VS.NETtimes New Roman'; mso-hansi-font-family: 'times New Roman'">的designertimes New Roman'; mso-hansi-font-family: 'times New Roman'">中使用这些对象时,如何发现错误并调试代码。我同时也将描述在使用设计界面操作控件时有可能发生的一系列事件。
times New Roman'; mso-hansi-font-family: 'times New Roman'">为了展示当你改变控件的值,或者拖一个控件到设计界面时如何检查错误并调试代码,我们将创建一个基于WinFormtimes New Roman'; mso-hansi-font-family: 'times New Roman'">的例子,它有一个stringtimes New Roman'; mso-hansi-font-family: 'times New Roman'">属性。当我们改变了属性的值时,我们将步入调试器中。
times New Roman'; mso-hansi-font-family: 'times New Roman'">建立环境
times New Roman'; mso-hansi-font-family: 'times New Roman'">第一步是建立一个容纳我们的代码的环境,我们将创建:
·times New Roman'; mso-hansi-font-family: 'times New Roman'">一个空的解决方案来容纳我们的工程
·一个WinForm程序来使用我们的自定义控件
建立空解决方案
1.times New Roman'"> 在File菜单中点击New,然后选择Blank Solution。
times New Roman'">2.times New Roman'"> 将Solution命名为designtimeDebugging
times New Roman'">3.times New Roman'"> 将此Solution放在c: designtimeDebugging。如下图:
创建空间库
times New Roman'; mso-hansi-font-family: 'times New Roman'">由于我们的目标是示范如何调试而非如何创建自定义控件,因此我们不关心控件创建的细节。我们将创建一个具有一个属性的简单的Formtimes New Roman'; mso-hansi-font-family: 'times New Roman'">。
times New Roman'; mso-hansi-font-family: 'times New Roman'">建立控件工程
times New Roman'">1.times New Roman'"> times New Roman'; mso-hansi-font-family: 'times New Roman'">在Filetimes New Roman'; mso-hansi-font-family: 'times New Roman'">菜单中点击Newtimes New Roman'; mso-hansi-font-family: 'times New Roman'">,然后点击Projecttimes New Roman'; mso-hansi-font-family: 'times New Roman'">。
times New Roman'">2.times New Roman'"> times New Roman'; mso-hansi-font-family: 'times New Roman'">在左边的窗口里选Visual C# Projectstimes New Roman'; mso-hansi-font-family: 'times New Roman'">。
times New Roman'">3.times New Roman'"> times New Roman'; mso-hansi-font-family: 'times New Roman'">在右边的窗口里选Windows Control Librarytimes New Roman'; mso-hansi-font-family: 'times New Roman'">。
times New Roman'">4.times New Roman'"> times New Roman'; mso-hansi-font-family: 'times New Roman'">将控件库命名为Immedient.Windows.Formstimes New Roman'; mso-hansi-font-family: 'times New Roman'">,注意我们保持与微软相同的命名规则,唯一不同的是我们用“Immedienttimes New Roman'; mso-hansi-font-family: 'times New Roman'">”代替了“Systemtimes New Roman'; mso-hansi-font-family: 'times New Roman'">”。这样就定义了一个唯一的名字空间。
times New Roman'">5.times New Roman'"> times New Roman'; mso-hansi-font-family: 'times New Roman'">删除UserControl1times New Roman'; mso-hansi-font-family: 'times New Roman'">。
times New Roman'; mso-hansi-font-family: 'times New Roman'">添加一个新的Form
times New Roman'">1.times New Roman'"> times New Roman'; mso-hansi-font-family: 'times New Roman'">右击Control Librarytimes New Roman'; mso-hansi-font-family: 'times New Roman'">工程,点Addtimes New Roman'; mso-hansi-font-family: 'times New Roman'">,然后点Add Windows Formtimes New Roman'; mso-hansi-font-family: 'times New Roman'">。
times New Roman'">2.times New Roman'"> times New Roman'; mso-hansi-font-family: 'times New Roman'">将Formtimes New Roman'; mso-hansi-font-family: 'times New Roman'">命名为“Formtimes New Roman'; mso-hansi-font-family: 'times New Roman'">”。
times New Roman'">3.times New Roman'"> times New Roman'; mso-hansi-font-family: 'times New Roman'">在类定义中添加如下代码:
C#
private string _myText = "Hello";
/// <summary>
/// A Custom property used for this silly sample
/// </summary>
/// <value>A value of no meaning</value>
[
Description("A Custom property used for this silly sample"),
DefaultValue("Hello"),
Category("Appearance")
]
public string MyText
{
get{return _myText;}
set
{
if(_myText != value)
{
_myText = value;
}
}
}
times New Roman'; mso-hansi-font-family: 'times New Roman'">代码Description("A Custom property used for this silly sample")times New Roman'; mso-hansi-font-family: 'times New Roman'">将在属性页的下方为你的属性添加描述。这里的文字通常与Xmltimes New Roman'; mso-hansi-font-family: 'times New Roman'">注视文档中的相同。
DefaultValue("Hello")times New Roman'; mso-hansi-font-family: 'times New Roman'">定义了我们的属性的缺省值。如果属性的值与DefaultValuetimes New Roman'; mso-hansi-font-family: 'times New Roman'">不同,属性页将以黑体显示该值。同时,VS.NETtimes New Roman'; mso-hansi-font-family: 'times New Roman'">也将在InitializeComponent()times New Roman'; mso-hansi-font-family: 'times New Roman'">中生成设置该值的代码。这一点对于继承非常好用。如果没有派生类改变属性,那么它们都将继承来自基类的改变。
Category("Appearance")times New Roman'; mso-hansi-font-family: 'times New Roman'">被用来将属性在属性页中排序。
times New Roman'; mso-hansi-font-family: 'times New Roman'">
times New Roman'; mso-hansi-font-family: 'times New Roman'">待续……
<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>