人生是一场不能存盘的RPG,我只能尽量多搞几个Screenshot

May 14, 2006

VS2005的Code Snippets

Filed under: Visual Studio

1.如何使用

可以使用菜单: Edit->ItelliSencse->Insert Smnippet 或使用Context meun :Insert Snippet也可以使用快捷键:Ctrl+K,Ctrl+X

2.使用很简单,关键是可以定制.使用菜单Tools->Code Snippet Manager可以看到Code Snippet所在的路径.

C:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033\Visual C#

所有的code snippets文件都是xml格式,并以.snippet结尾

文件中有两个核心的节点用来实现功能

<Header>节点用来描述code snippet的title,快捷键,描述,比如using.snippet

<Header>
   <Title>using</Title>
   <Shortcut>using</Shortcut>
   <Description>Code snippet for using statement</Description>
   <Author>Microsoft Corporation</Author>
   <SnippetTypes>
    <SnippetType>Expansion</SnippetType>
    <SnippetType>SurroundsWith</SnippetType>
   </SnippetTypes>
  </Header>

<Snippet>用来描述要生成的code,以using.snippet为例:

它会生成如下代码

using(resource)
{
 
}

用户可以把resource替换为自己的代码,xml中的描述如下

<Snippet>
   <Declarations>
    <Literal>
     <ID>resource</ID>
     <ToolTip>Resource to use</ToolTip>
     <Default>resource</Default>
    </Literal>
   </Declarations>
   <Code Language="csharp"><![CDATA[using($resource$)
 {
  $selected$ $end$
 }]]>

   </Code>
  </Snippet>

<Literal>部分定义了可替换的部分的id和default

[!CDATA]部分定义了要生成的代码模板,

参考资料

Code Snippets in Visual Studio 2005 讲解Code Snippets的使用和自定义,并提供了一个例子

Advanced Basics: IntelliSense Code Snippets — MSDN Magazine, April 2006

文章中的附件提供了几个例子,值得参考,同时还提供了几个Code Snippet编辑器的地址,用到时再看吧

 MSDN Code Snippet Schema Reference

Comments »

The URI to TrackBack this entry is: http://recordsome.blogsome.com/2006/05/14/p8/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham