Code Snippets in Visual Studio 2010

有两种类型的代码段:

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

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

创建自定义代码段

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

  1. <CodeSnippet Format="1.0.0"
  2. xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  3. <Header>
  4. <Title>title</Title>
  5. <Author>author</Author>
  6. <Shortcut>shortcut</Shortcut>
  7. <Description>description</Description>
  8. <SnippetTypes>
  9. <SnippetType>SurroundsWith</SnippetType>
  10. <SnippetType>Expansion</SnippetType>
  11. </SnippetTypes>
  12. </Header>
  13. <Snippet>
  14. <Declarations>
  15. <Literal>
  16. <ID>name</ID>
  17. <Default>value</Default>
  18. </Literal>
  19. </Declarations>
  20. <Code Language="XML">
  21. <![CDATA[<test>
  22. <name>$name$</name>
  23. $selected$ $end$</test>]]>
  24. </Code>
  25. </Snippet>
  26. </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部分的全部代码如下:

  1. <Code Language="CSharp">
  2. <![CDATA[
  3. try
  4. {
  5. }
  6. catch($ExceptionName$)
  7. {
  8. }
  9. finally
  10. {
  11. }
  12. ]]>
  13. </Code>

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

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <CodeSnippet Format="1.0.0"
  3. xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  4. <Header>
  5. <Title>Try Catch Finally</Title>
  6. <Author>umair</Author>
  7. <Shortcut>trycf</Shortcut>
  8. <Description>Adds a try-catch-finally block</Description>
  9. <SnippetTypes>
  10. <SnippetType>Expansion</SnippetType>
  11. </SnippetTypes>
  12. </Header>
  13. <Snippet>
  14. <Declarations>
  15. <Literal>
  16. <ID>ExceptionName</ID>
  17. <Default>Exception</Default>
  18. </Literal>
  19. </Declarations>
  20. <Code Language="CSharp">
  21. <![CDATA[
  22. try
  23. {
  24. }
  25. catch($ExceptionName$)
  26. {
  27. }
  28. finally
  29. {
  30. }
  31. ]]>
  32. </Code>
  33. </Snippet>
  34. </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提示),如下图所示:

输入try

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

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

插入代码段菜单

图 2 插入代码段菜单

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

插入TryCatchFinally自定义代码段

图 3 插入TryCatchFinally自定义代码段

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

原文名:Code Snippets in Visual Studio 2010

原文地址:https://www.cnblogs.com/baiyu/p/2079955.html