如何找到(并在必要时修复)我创建的代码片段?

时间:2015-05-12 21:28:07

标签: visual-studio-2010 visual-studio code-snippets vsinstaller

我主要使用this article作为指导,创建了一个代码片段(许多中的第一个,希望如此)。

似乎有效。以下是我采取的步骤:

首先,我使用代码片段(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,双击它,然后按照以下步骤将其安装到“我的代码片段”文件夹中:

enter image description here

enter image description here

enter image description here

它表示代码段 安装在合理的位置:

enter image description here

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

enter image description here

当我选择工具&gt; Code Snippets Manager ...,我可以导航到Visual C#&gt;我的代码片段,但它是空的。

enter image description here

当我使用Code Snippets Manager的“导入”按钮并导航到代码段的位置并尝试添加代码段文件时,我得到“所选的代码段文件无效。

为什么它告诉我它安装成功,当它显然没有(或它隐藏在哪里)?什么火焰箍我忽略了弹射自己?

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

更新

显然,命名“manifest”文件* .vscontent“不是问题。可能是 问题,但它不是 问题,因为我把它命名为与.snippets文件相同(扩展名除外),再次完成安装过程,得到了相同的结果:看似成功,实际道德化。

默认情况下,在选择要放置代码段的类别时,代码管理程序管理器会在“Microsoft Visual Studio Tools for Applications 2005&gt; My Code Snippets”中添加一个复选框。我之前没有注意到这一点,并勾选了最顶层的“我的代码片段”;这次我保留了默认选择,加上我的首选位置/类别PLUS“Visual C#”

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

1 个答案:

答案 0 :(得分:0)

Here's an easier way (and, as a bonus, it works):

Download the Snippets Designer:

enter image description here

Write the code directly in Visual Studio:

<script>
    var ecwid_sso_profile = '{{ message }} {{ payload  }} {{ timestamp }}' ;    
</script>

Right-click and select "Export as Snippet"

This puts the code in the Snippet Designer. Here's what it looks like after setting a few properties, and electing which parts of the code would be replaceable:

enter image description here

This is quite easy - sure beats the "olde-fashioned" way I was trying (which would have been okay, had it worked).

All I had to do was right-click the elements of code I wanted to replace on adding a new snippet, set the snippet's shortcut and description properties in Solution Explorer, save it, and voila!

Now mashing Ctrl+K, X in VS shows the snippet in "My Code Snippets" along with its description and shortcut:

enter image description here

Selecting it adds the snippet with the replacement strings highlighted:

enter image description here