0

我主要使用本文作为指导创建了一个代码片段(希望是众多代码片段中的第一个) 。

它似乎奏效了。以下是我采取的步骤:

首先,我使用代码片段 (HtmlTableRowWithTwoCells.snippet) 创建了这个文件:

<?xml version="1.0" encoding="utf-8" ?> 
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/CodeSnippet">
    <CodeSnippet Format="1.0.0">
         <!-- The Title of the snippet, this will be shown in the snippets manager. -->
          <Title>Create a 2-Cell HTML Table Row</Title>

          <!-- The description of the snippet. -->
          <Description>Creates a 2-Cell Row to be added to an HtmlTable</Description>

          <!-- The author of the snippet. -->
          <Author>Warble P. McGorkle for Platypi R Us (Duckbills Unlimited)</Author>

          <!-- The set of characters that must be keyed in to insert the snippet. -->
          <Shortcut>row2</Shortcut>          

          <!-- 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>

        <Declarations>
            <Literal>
              <ID>RowName</ID>
              <ToolTip>Enter the Row instance name</ToolTip>
              <Default>RowName</Default>
            </Literal>
            <Literal>
              <ID>Cell1Name</ID>
              <ToolTip>Enter the name for Cell 1</ToolTip>
              <Default>Cell1Name</Default>
            </Literal>
            <Literal>
              <ID>Cell2Name</ID>
              <ToolTip>Enter the name for Cell 2</ToolTip>
              <Default>Cell2Name</Default>
            </Literal>
        </Declarations>

            <!-- Sepecify the code language and the actual snippet content. -->
            <Code Language="CSharp" Kind="any">
                <![CDATA[

                    var $RowName$ = new HtmlTableRow();
                    var $Cell1Name$ = new HtmlTableCell();
                    var $Cell2Name$ = new HtmlTableCell();
                    $RowName$.Cells.Add($Cell1Name$);
                    $RowName$.Cells.Add($Cell2Name$);

                ]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

然后,我创建了这个“清单”文件(.vscontent):

<?xml version="1.0" encoding="utf-8" ?>
<VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005" >
  <Content>
    <FileName>HtmlTableRowWithTwoCells.snippet</FileName>
    <DisplayName>Inserts a 2-Cell HTML Table Row</DisplayName>
    <Description>Inserts a 2-Cell Row to be added to an HtmlTable</Description>
    <FileContentType>Code Snippet</FileContentType>
    <ContentVersion>2.0</ContentVersion>
    <Attributes>
      <Attribute name="lang" value="csharp"/>
    </Attributes>
  </Content>
</VSContent>

我将这两个文件压缩在一起,将扩展名从 .zip 重命名为 .vsi,双击它,然后通过以下步骤将其安装到此处的“我的代码片段”文件夹中:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

它表明该片段安装在合理的位置:

在此处输入图像描述

然而,当我尝试在 VS 中添加代码片段时,我看到的唯一类别是这些(没有“我的代码片段”):

在此处输入图像描述

当我选择工具 > 代码片段管理器...时,我可以导航到 Visual C# > 我的代码片段,但它是空的。

在此处输入图像描述

当我使用代码片段管理器的“导入”按钮并导航到片段的位置并尝试添加片段文件时,我得到“选择的片段文件无效。

为什么它告诉我它安装成功,而它显然没有(或者它藏在哪里)?我忽略了哪个燃烧的箍让自己通过了?

“清单”文件的“奇怪”名称可能是问题吗?“.vscontent”对我来说似乎很奇怪,但这就是上面引用的文章所说的命名它。也许这只是疏忽,它真的应该是 [snippetName].vscontent?

更新

显然,将“清单”文件命名为 *.vscontent” 不是问题。也许这是一个问题,但不是问题,因为我将它命名为与 .snippets 文件相同(扩展名除外),通过安装再次处理,并得到相同的确切结果:表面上的成功,实际的士气低落。

BTW, by default, when choosing a category into which to place the snippet, the Code Snipeets Manager puts a checkbox in "Microsoft Visual Studio Tools for Applications 2005 > My Code Snippets". 我之前取消勾选并勾选了最上面的“我的代码片段”;这次我保留了默认选择,加上我的首选位置/类别加上“Visual C#”

但是,唉,在 VS 中似乎通过 Ctrl+K、X 显示的唯一类别是 C#,并且预期的快捷方式(“row2”)不会出现在片段下拉列表中。

4

1 回答 1

0

这是一种更简单的方法(而且,作为奖励,它有效):

下载片段设计器:

在此处输入图像描述

直接在 Visual Studio 中编写代码:

var RowName = new HtmlTableRow();
var Cell1Name = new HtmlTableCell();
var Cell2Name = new HtmlTableCell();
RowName.Cells.Add(Cell1Name);
RowName.Cells.Add(Cell2Name);

右键单击并选择“导出为代码段”

这会将代码放入片段设计器中。这是设置一些属性并选择代码的哪些部分可以替换后的样子:

在此处输入图像描述

这很容易 - 肯定会击败我尝试的“老式”方式(如果它有效,那本来可以的)。

我所要做的就是右键单击要在添加新片段时替换的代码元素,在解决方案资源管理器中设置片段的快捷方式和描述属性,保存它,瞧!

现在在 VS 中混合 Ctrl+K、X 会显示“我的代码片段”中的片段及其描述和快捷方式:

在此处输入图像描述

选择它会添加突出显示替换字符串的代码段:

在此处输入图像描述

于 2015-05-12T22:58:52.073 回答