一、名词解释

Code Snippet,代码模板,是一种快速生成代码的快捷方式,使用它可以有效地提高编程效率。

编程中可以使用Visual Studio提供的预先设置好的Code Snippet,也可以根据需要创建自己的Code Snippet。

二、使用方法演示

使用code snippet创建属性:

1. 输入prop,出现下图所示的提示:

2. 连按两下Tab键,得到如下代码:

3. 按一下Tab键,可以在橙色背景色的可更改字段之间来回跳转,编辑后得到自定义的属性:

public string FirstName { get; set; }

三、创建自己的Code Snippet

以创建具有通知功能的属性为例,该种属性是基于Caliburn.Micro框架的,写在ViewModel中,可与View界面上的控件进行绑定。

  1. 在Visual Studio的Tools菜单里,找到Code Snippets Manager,

  2. 在Language下拉框中选择Visual C#,

  3. 在Location下面的很多文件夹中,找到Visual C#文件夹,可以看到很多code snippet文件,根据路径打开该文件夹,

  4. 将propfull.snippet文件复制出来,我们将基于它修改得到自己的code snippet,重命名为propcn.snippet,cn是Caliburn.Micro和Notification的缩写,

  5. 打开propcn.snippet,开始修改,

  6. 修改<Header>中的代码为:

<Title>propcn</Title>
<Shortcut>propcn</Shortcut>
<Description>Code snippet for Notification property in Caliburn.Micro</Description>

修改<Code>中的代码为:

<Code Language="csharp">
<![CDATA[private $type$ $field$; public $type$ $property$
{
get { return $field$;}
set
 {
  $field$ = value;
   NotifyOfPropertyChange(() => $property$);
  }
}
$end$]]>
</Code>

7.  保存propcn.snippet,并将该新的code snippet文件剪切到Visual C#文件夹下,至此就创建好了自己的code snippet,试试打开Visual Studio使用它:

输入propcn,连按两下Tab键,得到如下代码片段,修改为自己需要的属性即可啦。

private string _firstName;

public string FirstName
{
get { return _firstName; }
set
{
_firstName = value;
NotifyOfPropertyChange(() => FirstName);
}
}

四、常用的Code Snippet

ctor → 构造函数

for → for循环

prop → 简化类型的属性

propfull → 完整属性

propdp → 依赖属性

五、添加XAML code snippet

主要步骤同上,找到XAML code snippet的文件夹,将新建的代码模板文件放入,在XAML中编程时输入缩写再敲击两下Tab键。

例如,2行2列的Grid模板:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>2X2 Grid</Title>
<Author>Microsoft Corporation</Author>
<Description>XAML snippet for a 2X2 Grid</Description>
<HelpUrl></HelpUrl>
<Shortcut>2by2Grid</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>Height1</ID>
<ToolTip>Height of row 0</ToolTip>
<Default>*</Default>
<Function>
</Function>
</Literal>
<Literal Editable="true">
<ID>Height2</ID>
<ToolTip>Height of row 1</ToolTip>
<Default>*</Default>
<Function>
</Function>
</Literal>
<Literal Editable="true">
<ID>Width1</ID>
<ToolTip>Width of column 0</ToolTip>
<Default>*</Default>
<Function>
</Function>
</Literal>
<Literal Editable="true">
<ID>Width2</ID>
<ToolTip>Width of column 1</ToolTip>
<Default>*</Default>
<Function>
</Function>
</Literal>
</Declarations>
<Code Language="XAML">
<![CDATA[
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="$Width1$"/>
<ColumnDefinition Width="$Width2$"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="$Height1$"/>
<RowDefinition Height="$Height2$"/>
</Grid.RowDefinitions>
</Grid>
]]>
</Code>
</Snippet>
</CodeSnippet>

也可以下载一些现成的模板文件,例如CodePlex Archive 的XAML snippets:https://archive.codeplex.com/?p=xamlsnippets

创建自己的Code Snippet(代码模板)的更多相关文章

  1. 如何创建 Code Snippet

    比如有一行自定义代码段: @property (nonatomic,copy) NSString *<#string#>; 需要添加到 Code Snippet 上,以帮助开发人员开发更便 ...

  2. 善用VS中的Code Snippet来提高开发效率

    http://www.cnblogs.com/anderslly/archive/2009/02/16/vs2008-code-snippets.html http://www.cnblogs.com ...

  3. 善用VS中的Code Snippet来提高开发效率 分类: C# 2015-01-22 11:06 69人阅读 评论(0) 收藏

    前言  在谈谈VS中的模板中,我介绍了如何创建项目/项模板,这种方式可以在创建项目时省却不少重复性的工作,从而提高开发效率.在创建好了项目和文件后,就得开始具体的编码了,这时又有了新的重复性工作,就是 ...

  4. wpf Route Event Code Snippet

    将下面内容保存为snippet后缀文件,通过vs的代码片段管理工具导入即可,快捷键请按需修改: <?xml version="1.0" encoding="utf- ...

  5. iOS programming Code Snippet Library

    iOS programming  Code Snippet Library  The freebie code comes from the code snippet library. 代码来自cod ...

  6. 关于VS2015中的code snippet无法使用的问题

    什么是code snippet? Code snippets are small blocks of reusable code that can be inserted in a code file ...

  7. VS自定义代码块Code Snippet

    一  .简述 我们在开发当中,避免不了一些重复的开发工作,在你漫长的开发以及学习当中,你会发现有这么一部分代码是你时常会使用到的.我想这个工具也是针对这个原因出来的吧,它就是预先把你需要的这部分代码的 ...

  8. VS里的 代码片段(Code snippet)很有用,制作也很简单

    工欲善其事必先利其器,而 Visual Studio 就是我们的开发利器. 上一篇文章,介绍了一个很棒的快捷键,如果你还没用过这个快捷键,看完之后应该会豁然开朗.如果你已经熟练的应用它,也会温故而知新 ...

  9. 使用 Code Snippet 简化 Coding

    在开发的项目的时候,你是否经常遇到需要重复编写一些类似的代码,比如是否经常会使用 for.foreach ? 在编写这两个循环语句的时候,你是一个字符一个字符敲还是使用 Visual Studio 提 ...

随机推荐

  1. tcp连接需要注意的问题

    当有子进程时,子进程终止时会返回SIGCHLD信号,默认忽略,此时会有僵尸进程. 处理方法: 捕获信号,并waitpid. 当慢系统调用被中断时(如信号中断),有些系统不会自动重启调用,此时系统调用可 ...

  2. Monkey原理初步和改良优化--Android自动化测试学习历程

    章节:自动化基础篇——Monkey原理初步和改良优化(第三讲) 主要讲解内容与笔记: 一.理论知识: 直接看文档,来了解monkey的概念.基本原理,以及如何使用. First,what is And ...

  3. PAT L3-008 喊山(广搜)

    喊山,是人双手围在嘴边成喇叭状,对着远方高山发出“喂—喂喂—喂喂喂……”的呼唤.呼唤声通过空气的传递,回荡于深谷之间,传送到人们耳中,发出约定俗成的“讯号”,达到声讯传递交流的目的.原来它是彝族先民用 ...

  4. linux 下 php 安装 ZeroMQ 扩展

    一.下载安装源码包 ZeroMQ源码包下载地址: http://zeromq.org/area:download 如:zeromq-4.1.4.tar.gz   php的zmq扩展源码包 https: ...

  5. C语言字符串操作函数实现

    1.字符串反转 – strRev void strRev(char *str) { assert(NULL != str);   int length=strlen(str); ; while(end ...

  6. Python.__getattr__Vs__getattribute__

    __getattr__ Vs __getattribute__ class Fish(object): def __getattr__(self, key): if key == 'color': p ...

  7. @RequestMapping 介绍

    RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径. RequestMapping注解有六个属性,下面我们把她 ...

  8. msys2 显示git branch

    在.bashrc或.bash_profile中添加以下内容 function parse_git_branch () { git branch 2> /dev/null | sed -e '/^ ...

  9. Winpython环境下mayavi配置

    Winpython环境下mayavi配置 在pythonxy中会直接有mayavi软件包,但是所附带的杂包实在太多.本人一直用的是window下的winpython或者linux下的anaconda来 ...

  10. HTTP 500.21 处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”

    问题:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler” IIS7下部署网站出现错误,原因是先安装的II ...