提高效率 用好Visual Studio 2010自定义代码段

    技术2025-09-25  68

    (来源:http://developer.51cto.com/art/201006/208136.htm )

    我在演讲时发现这个功能也相当有用,不用现场敲代码,直接调出非常方便。Visual Studio 2010增强了自定义代码段功能,使创建自定义代码段的操作更加简单了。

    有两种类型的代码段:

    ◆在游标中插入的Expansion自定义代码段

    ◆围绕选定代码的SurroundsWith自定义代码段

    创建自定义代码段

    首先在项目中插入一个新的XML文件,取名为TryCatchFinally.snippet,注意文件名的后缀是.snippet,然后在编辑器窗口点击右键,选择“插入代码段”*“代码段”,创建一个基本的XML代码段模板,代码如下:

    < CodeSnippet   Format = "1.0.0"                 xmlns = "http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet" >     < Header >        < Title > title </ Title >        < Author > author </ Author >        < Shortcut > shortcut </ Shortcut >        < Description > description </ Description >        < SnippetTypes >          < SnippetType > SurroundsWith </ SnippetType >          < SnippetType > Expansion </ SnippetType >        </ SnippetTypes >     </ Header >     < Snippet >        < Declarations >          < Literal >            < ID > name </ ID >            < Default > value </ Default >          </ Literal >        </ Declarations >        < Code   Language = "XML" >          <![CDATA[<test>        <name>$name$</name>         $selected$ $end$</test>]]>        </ Code >     </ Snippet >   </ CodeSnippet >  

    正如你所看到的,默认的模板包括两个代码段类型,因为我这里是要创建一个Expansion代码段,因此去掉<SnippetType>SurroundsWith</SnippetType>标签。

    然后将title改为“Try Catch Finally”,Shortcut改为“trycf”,Description改为“Adds a try-catch-finally block”。

    Literal标签定义了代码段中可编辑的值,在try-catch-finally代码段中,我们希望用户可修改捕获到的异常 (Exception)类型,ID标签是可编辑值的名字,因此将其改为“ExceptionName”,Default标签指定了可编辑字段的默认值,我 想捕获所有的异常,因此我将它的值改为“Exception”。

    最后,code小节部分包括代码段插入时自动填充的代码,将language改为“CSharp”,code部分的全部代码如下:

      < Code   Language = "CSharp" >          <![CDATA[  try   {       }   catch($ExceptionName$)   {       }   finally   {       }         ]]>        </ Code >  

    TryCatchFinally.snippet文件的最终代码如下:

    <? xml   version = "1.0"   encoding = "utf-8"   ?>   < CodeSnippet   Format = "1.0.0"                 xmlns = "http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet" >     < Header >        < Title > Try Catch Finally </ Title >        < Author > umair </ Author >        < Shortcut > trycf </ Shortcut >        < Description > Adds a try-catch-finally block </ Description >        < SnippetTypes >          < SnippetType > Expansion </ SnippetType >        </ SnippetTypes >     </ Header >     < Snippet >        < Declarations >          < Literal >            < ID > ExceptionName </ ID >            < Default > Exception </ Default >          </ Literal >        </ Declarations >        < Code   Language = "CSharp" >          <![CDATA[  try   {       }   catch($ExceptionName$)   {       }   finally   {       }         ]]>        </ Code >     </ Snippet >   </ CodeSnippet >  

    在Visual Studio中载入自定义代码段

    在Visual Studio中有两种方法载入上面的自定义代码段:

    最直接的方法是将.snippet文件放在Visual Studio的代码段目录下,默认位置是C:/Users/<UserName>/Documents/Visual Studio 2010/Code Snippets/,这个目录会根据所使用的语言生成对应的子目录,如我们这里使用的C#,因此应该将自定义代码段文件放在Visual C#子目录下,Visual Studio会自动发现新放进去的.snippet文件,无需重启Visual Studio。

    第二种方法是将.snippet文件导入到Visual Studio中,选择“工具”*“代码段管理器”(Ctrl+K,Ctrl+B),在代码段管理器中,点击“导入”按钮,浏览到.snippet文件所在位置,选择它,然后点击“确定”。

    使用自定义代码段

    在Visual Studio的编辑器中,输入自定义代码段名字的前几个字符,按Tab键,在弹出的代码提示窗口中便可看到前面自定义的代码段名称(快捷名称和完全名称Tip提示),如下图所示:

    图 1 输入try,在弹出的代码提示窗口中可看到自己定义的代码段名

    你也可以在编辑器中按CTRL+K,再按CTRL+X调出“插入代码段”菜单,如下图所示:

    图 2 插入代码段菜单

    选择菜单中的“My Code Snippets”,再选择前面添加的TryCatchFinally自定义代码段,如下图所示:

    图 3 插入TryCatchFinally自定义代码段

    正如你所看到的,在Visual Studio 2010中,添加自定义代码段,装载自定义代码段文件和使用自定义代码都变得更加简单了,这一提高生产力的功能你应该多多使用。

    最新回复(0)