【问题标题】:How can I find (and rehabilitate, if necessary) the Code Snippet I created?如何找到(并在必要时修复)我创建的代码片段?
【发布时间】:2015-07-23 23:13:41
【问题描述】:

我创建了一个代码 sn-p(希望是第一个),主要使用 this article 作为指导。

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

首先,我使用代码 sn-p (HtmlTableRowWithTwoCells.sn-p) 创建了这个文件:

<?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,双击它,然后通过以下步骤将其安装到此处的“我的代码片段”文件夹中:

并且它表明sn-p 安装在一个合理的位置:

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

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

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

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

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

更新

显然,将“清单”文件命名为 *.vscontent” 不是问题。可能是 a 问题,但不是 问题,因为我将其命名为与 .sn-ps 文件相同(扩展名除外),再次进行了安装过程,并得到了完全相同的结果:表面上成功,实际士气低落。

btw,默认情况下,在选择要放置SN-P的类别时,代码Snipeets Manager将在“Microsoft Visual Studio工具”中的“应用程序2005> 2005>”代码片段“中进行复选框。我之前取消勾选并勾选了最上面的“我的代码片段”;这次我保留了默认选择,加上我的首选位置/类别加上“Visual C#”

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

【问题讨论】:

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


    【解决方案1】:

    这里有一个更简单的方法(作为奖励,它很有效):

    下载片段设计器:

    直接在 Visual Studio 中编写代码:

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

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

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

    这很容易 - 肯定比我尝试的“老式”方式要好(如果可行的话,那本来可以的)。

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

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

    选择它会添加突出显示替换字符串的 sn-p:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-17
      • 2015-04-04
      • 2012-12-04
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      • 1970-01-01
      相关资源
      最近更新 更多