一、名词解释

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. Selenium原理初步--Android自动化测试学习历程

    章节:自动化基础篇——Selenium原理初步(第五讲) 注:其实所有的东西都是应该先去用,但是工具基本都一样,底层都是用的最基础的内容实现的,测试应该做的是: (1)熟练使用工具,了解各个工具的利弊 ...

  2. Linked dylibs built for GC-only but object files built for retain/release for architecture x86_64

    编译开源Xcode 插件 SCXcodeSwitchExpander 源码地址: https://github.com/stefanceriu/SCXcodeSwitchExpander 编译环境:X ...

  3. ios 进入后台 一段时间在进入前台 动画消失

    http://www.cnblogs.com/YouXianMing/p/3670846.html

  4. 文件Move操作

    #coding=utf-8 import os import shutil stra = "G:/should/v3/a" strb = "G:/should/v3/b& ...

  5. linux引导系统

    一.linux引导系统 1.选择操作系统 /etc/grub.conf 设置grub引导装载程序口令,使用单用户模式时必须输入此密码 password --md5 md5后的密码字符串(可以通过gru ...

  6. MVC仓储使用join

    代码: var result = from mpc in this.Context.Set<Domain.S_MENU_PURVIEWCODE>() join menu in this.C ...

  7. Python3编程技巧

    高效处理数据类型方法: In []: from random import randint In []: data=[randint(-,) )] In []: data Out[]: [-, -, ...

  8. Judy Array - Example

    “ In computer science and software engineering, a Judy array is a data structure that has high perfo ...

  9. [NOI.AC]DELETE(LIS)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABRMAAASJCAYAAABLtYu4AAAgAElEQVR4Xuzdf2xTd74n/PeqI/NsNB ...

  10. laravel表单图片上传

    1.视图 2.控制器