如何在.NET中调试设计时(design-time)控件(上)

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

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>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值