Visual Studio 的代码片段(Code Snippet)功能

    1、概述

     在 Visual Studio 中,有一个快捷编辑代码的功能,比如当我们在 VS 编辑器(C#)中写出

关键字 foreach 后,敲击一下 Tab 键,VS 就帮我们自动补全:(插入代码段的快捷键 Ctrl + K + X

   foreach (var item in collection)
   {
       
   }

        我的电脑是  Windows8 的系统,编辑器版本是 Visual Studio 2012,编辑器默认安装的代码片段路径:

     C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC#\Snippets\2052\Visual C#


     同时 VS 也提供了方便开发者自定义代码片段,文件默认存放路径:

     C:\Users\boqiong\Documents\Visual Studio 2012\Code Snippets\Visual C#\My Code Snippets

    

打开 VS 的“代码段管理器”: 工具 ---> 代码段管理器  (快捷键:Ctrl + K + B

可以把自定义的代码段文件通过点击“导入”按钮,添加到自己的代码段文件夹(My Code Snippets)

  2、编辑自己的代码段       

    snippet 是xml文件,以.snippet后缀名,下面的自定义代码段的文件名为:sldpc.snippet

    这里自定义一个 Silverlight 中的依赖属性代码段:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Silverlight DependencyProperty</Title>
      <Shortcut>sldpc</Shortcut>
      <Description>Silverlight 依赖属性(DependencyProperty)的定义</Description>
      <Author>dbq</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>type</ID>
          <ToolTip>属性类型</ToolTip>
          <Default>int</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>property</ID>
          <ToolTip>属性名称</ToolTip>
          <Default>Name</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>defaultvalue</ID>
          <ToolTip>默认值</ToolTip>
          <Default>0</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="false">
          <ID>ownerclass</ID>
          <ToolTip>该依赖属性所属的类</ToolTip>
          <Default>ClassNamePlaceholder</Default>
          <Function>ClassName()</Function>
        </Literal>
        <Literal Editable="true">
          <ID>description</ID>
          <ToolTip>描述</ToolTip>
          <Default>该依赖属性的描述</Default>
          <Function>
          </Function>
        </Literal>
      </Declarations>
      <Code Language="csharp">
        <![CDATA[
#region $property$ (DependencyProperty)

/// <summary>
/// $description$
/// </summary>
public $type$ $property$
{
    get { return ($type$)GetValue($property$Property); }
    set { SetValue($property$Property, value); }
}
public static readonly DependencyProperty $property$Property = 
    DependencyProperty.Register("$property$", typeof($type$), typeof($ownerclass$), 
    new PropertyMetadata($defaultvalue$, new PropertyChangedCallback(On$property$Changed)));
    
private static void On$property$Changed(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{    
    // 获取自身的引用
    $ownerclass$ source = ($ownerclass$)sender;

    // 处理逻辑
    $type$ newValue = ($type$)args.NewValue;
}

#endregion
$end$]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

  在 VS 中,自定义一个类 MyClass,该类继承自 DependencyObject,在页面中敲如 sldpc,弹出智能提示:

  敲一下 Tab 键,则自动补全代码:

    继续点击 Tab 键,可以在关键字之间切换。


节点说明:

<Title>表示此代码段标题

<Shortcut>设置快捷键

<Description>对代码段的描述

<SnippetTypes>可以包含多个<SnippetType>其取值有三种Expansion、SurroundsWith、Refactoring 。Expansion允许代码插入在光标处;SurroundsWith允许代码围绕在选中代码两边;Refactoring指定了C#重构过程中所使用的Snippet,在自定义Snippet中不能使用。如果该值不做设置,则Snippet可以放在任何地方。

<Snippet>节点是实现代码管理的地方,其包含四个子节点<Code><Declarations><Imports><References>

1.<Code>

包含<![CDATA[]]>中,放置模版代码,此节点设置Language(C# VB XML),Kind(类型:如方法体,方法声明),Delimiter(分隔符,默认值是$)

2.<Declarations>

包含多个<Literal>和<Object>节点,<Literal>用于指定文本值<Object>用于声明模版对象。笔者自理解为一个函数。以便code调用.

3.<Imports>

引入命名空间,只支持vb .  - -#.

4.<References>

添加程序集引用,只支持vb .  - -#.

函数只适合于C# 总共3个函数

1.GenerateSwitchCases(EnumerationLiteral),根据枚举生成switch代码.

2.ClassName() 返回类名:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="false">
          <ID>classname</ID>
          <Function>ClassName()</Function>
          <Default>ClassNamePlaceholder</Default>
        </Literal>
      </Declarations>
      <Code Language="csharp">
        <!--[CDATA[  
  public class $EventHandlerType$ 
  {
     //to do ....
  }]]-->
      </Code>
    </Snippet>
   </CodeSnippet>
  </CodeSnippets>

3.SimpleTypeName(TypeName),在Snippet所在的上下文中推断出TypeName参数的最简单形式:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="false">
          <ID>EventHandlerType</ID>
          <Function>SimpleTypeName(global::System.EventHandler)</Function>
        </Literal>
      </Declarations>
      <Code Language="csharp">
        <!--[CDATA[  
  public class $EventHandlerType$  
 {
     // to do ... 
    }
  }]]-->
      </Code>
    </Snippet>
   </CodeSnippet>
  </CodeSnippets>


3、扩展阅读

    http://blog.nerdplusart.com/archives/silverlight-code-snippets

原文地址:https://www.cnblogs.com/hebeiDGL/p/3136003.html