扩展 Visual Studio 第一部分 – 创建代码片段
学习如何通过创建自定义代码片段来扩展 Visual Studio 2010。
扩展 Visual Studio
本文是“扩展Visual Studio”系列文章的一部分。
第1部分 - 创建代码片段
第2部分 - 创建插件
第 3 部分 - 项目模板
引言
在本系列文章中,我将向您展示一些扩展 Visual Studio 的绝妙方法。 Visual Studio 具有惊人的可扩展性,您可以通过多种方式进行构建。
在本文中,我们将从扩展 Visual Studio 最简单的方法之一开始——创建代码片段。
什么是代码片段?
代码片段是一段预先格式化的代码,它被放入编辑器中,用于插入新的代码块或包装现有的代码块。 该片段可以包含一些可重用的代码段。 例如,创建一个空类并键入“ctor”。 当您键入时,IntelliSense 将为您缩小选择范围,在这里我们可以看到一个片段
现在按两次 Tab 键。 “ctor”片段创建类构造函数。 您可以创建各种各样的片段,让我们开始吧。
简介
首先,让我们定义我们希望该片段做什么。 查看下面的代码 - 在这里您可以看到来自我的 Apex 库 的两个通知属性。
private NotifyingProperty firstNameProperty =
new NotifyingProperty("FirstName", typeof(string), string.Empty);
public string FirstName
{
get { return (string)GetValue(firstNameProperty); }
set { SetValue(firstNameProperty, value); }
}
private NotifyingProperty secondNameProperty =
new NotifyingProperty("SecondName", typeof(string), string.Empty);
public string SecondName
{
get { return (string)GetValue(secondNameProperty); }
set { SetValue(secondNameProperty, value); }
}
我在我的代码中到处都使用这些属性 - 所以这是代码片段的理想候选者。 大部分代码在每种情况下都是相同的,唯一会改变的是
- 属性的名称
- 属性的类型
创建片段
在示例项目中,我已将片段文件添加到空的 C# 项目中 - 但是,我们不需要任何特定类型的项目来创建片段,它只是一点 XML。 创建一个新的 XML 文件并将其重命名为 InsertApexNotifyingProperty.snippet。 使用下面的样板开始。
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/CodeSnippet">
<CodeSnippet Format="1.0.0">
<!-- The header contains information describing the snippet. -->
<Header>
<!-- The Title of the snippet, this will be shown in the snippets manager. -->
<Title>Insert Notifying Property</Title>
<!-- The description of the snippet. -->
<Description>Inserts an Apex Notifying Property.</Description>
<!-- The author of the snippet. -->
<Author>Dave Kerr</Author>
<!-- The set of characters that must be keyed in to insert the snippet. -->
<Shortcut>apexnp</Shortcut>
<!-- A URL for more help. -->
<HelpUrl>http://apex.codeplex.com</HelpUrl>
<!-- The set of snippet types we're dealing with - either Expansion or -->
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<!-- Now we have the snippet itself. -->
<Snippet>
<!-- Sepecify the code language and the actual snippet content. -->
<Code Language="CSharp" Kind="any">
<![CDATA[
// My first snippet!
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
您可能会发现此样板很有用 - 页面顶部有指向它的链接。
Visual Basic 注意事项: 如果您正在创建 VB 代码片段,请在“Code”标签中将“Language”从“CSharp”更改为“VB”。
那么我们在这里做了什么? 只是为该片段创建了一些摘要数据和一个非常基本的内容。
安装片段
选择“工具 > 代码片段管理器”,然后选择“导入” - 浏览到您新创建的片段。 现在在任何代码窗口中,您可以输入“apexnp”,您将得到注释“// 我的第一个片段”。 不错!
添加参数
让我们概括一下我们想要创建的代码。 将下面的代码插入到“Code
”标签中的片段文件中。
private NotifyingProperty PropertyNameProperty =
new NotifyingProperty("PropertyName", typeof(PropertyType), default(PropertyType));
public PropertyType PropertyName
{
get { return (PropertyType)GetValue(PropertyNameProperty); }
set { SetValue(PropertyNameProperty, value); }
}
我用红色和蓝色突出显示了关键点 - 实际上只需要替换两个代码段,即 PropertyName
和 PropertyType
字符串。
修改片段定义,使其看起来像这样
<!-- Now we have the snippet itself. -->
<Snippet>
<!-- Create any declarations that we use in the snippet. -->
<Declarations>
<Literal>
<ID>PropertyName</ID>
<ToolTip>Enter the property name</ToolTip>
<Default>PropertyName</Default>
</Literal>
<Literal>
<ID>PropertyType</ID>
<ToolTip>Enter the property name</ToolTip>
<Default>PropertyType</Default>
</Literal>
</Declarations>
<!-- Sepecify the code language and the actual snippet content. -->
<Code Language="CSharp" Kind="any">
<![CDATA[
private NotifyingProperty $PropertyName$Property =
new NotifyingProperty("$PropertyName$", typeof($PropertyType$), default($PropertyType$));
public $PropertyType$ $PropertyName$
{
get { return ($PropertyType$)GetValue($PropertyName$Property); }
set { SetValue($PropertyName$Property, value); }
}
]]>
</Code>
</Snippet>
我们添加了两个 Literal
标签。 Literal
在每个代码段中都被替换。 字面量的第一部分是 ID - 这是我们在 Code
标签中必须用美元符号括起来的。 第二部分是显示的工具提示。 最后一部分是片段的默认值。 让我们看看我们更新的片段的实际效果。
完美! 一切都按预期工作。
安装片段
我们可以通过首先创建另一个名为 .vscontent 的 XML 文件来为片段创建一个安装程序。 它应该看起来像这样
<?xml version="1.0" encoding="utf-8" ?>
<VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005" >
<Content>
<FileName>InsertApexNotifyingProperty.snippet</FileName>
<DisplayName>Insert Apex Notifying Property</DisplayName>
<Description>Inserts an Apex Notifying Property</Description>
<FileContentType>Code Snippet</FileContentType>
<ContentVersion>2.0</ContentVersion>
<Attributes>
<Attribute name="lang" value="csharp"/>
</Attributes>
</Content>
</VSContent>
Visual Basic 注意事项: 如果您需要安装 Visual Basic 片段 - 使用“vb”作为“attribute”标签的“value”部分。
最后要做的就是将 .vscontent 和 .snippet 文件添加到新的 *.zip 文件中,并将文件名从 zip 重命名为 vsi。 这将创建一个 VSI 安装程序 - 双击它,您将得到以下内容
重要提示:如果在运行安装程序时遇到异常,您是否安装了 Windows Phone Developer Toolkit? 如果是,请在 http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/6504fde1-b84d-4e4f-80f9-54f6a5596fc0/ 上发帖并引发骚动,以便 Microsoft 修复此错误!
如果您需要显示发布者信息,则必须对 VSI 文件进行签名,此处提供了更多详细信息:http://msdn.microsoft.com/en-us/library/ms246580.aspx。
最终想法
我希望您喜欢这篇文章,请关注我的博客以获取该系列下一篇文章的消息,www.dwmkerr.com。