【问题标题】:VS2010 Code Snippet Shortcut Not ShowingVS2010 代码片段快捷方式未显示
【发布时间】:2011-08-09 16:21:30
【问题描述】:

我在 VS2010 中创建了一个代码 sn-p。当我开始输入时,它没有显示为快捷方式。我称之为propnch。

当我使用 Ctrl-K、Ctrk-X 时可用,但当我刚开始输入 prop... 时,它没有显示为选项。

我是否错过了某处的某种设置?

我有屏幕截图,但我认为 SO 不允许您上传任何内容。

编辑:屏幕截图

我可以用 Ctrl-K、Ctrl-X 看到我的 sn-p(当我 ctrl-PrtScn 截屏时它变灰了)

但它不会与其他 sn-p 快捷方式一起出现。

sn-p 代码在这里(取自this tutorial),位于“Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets”文件夹中。

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
    <Header>
        <Title>propnch</Title>
        <Shortcut>propnch</Shortcut>
        <Description>Code snippet for property and backing field and ensure 
  that it invokes INotifyPropertyChanigng and INotifyPropertyChanged</Description>
        <Author>Abhishek</Author>
        <SnippetTypes>
            <SnippetType>Expansion</SnippetType>
        </SnippetTypes>
    </Header>
    <Snippet>
        <Declarations>
            <Literal>
                <ID>type</ID>
                <ToolTip>Property type</ToolTip>
                <Default>int</Default>
            </Literal>
            <Literal>
                <ID>property</ID>
                <ToolTip>Property name</ToolTip>
                <Default>MyProperty</Default>
            </Literal>
            <Literal>
                <ID>field</ID>
                <ToolTip>The variable backing this property</ToolTip>
                <Default>myVar</Default>
            </Literal>
        </Declarations>
        <Code Language="csharp"><![CDATA[

private $type$ $field$;

public $type$ $property$
{
    get 
    { 
        return $field$;
    }
    set 
    { 
        this.OnPropertyChanging("$property$");
        $field$ = value;
        this.OnPropertyChanged("$property$");
    }
}
$end$]]>
        </Code>
    </Snippet>
</CodeSnippet>

【问题讨论】:

  • 你可以通过点击编辑框上方的图片按钮来添加屏幕截图
  • 你能发布你的 .sn-p 吗?我已经成功复制了一个名为propnch 的sn-p,它工作正常。
  • 嗨,我已经添加了屏幕截图和sn-p代码。

标签: visual-studio-2010 code-snippets


【解决方案1】:

原来这是 VS2010 中 xml 编辑器的设计缺陷。在 C# 编辑器中,您只需键入快捷方式并按“制表符”。在 xml 编辑器中,它还需要两次击键。

To quote from the documentation:

To insert snippets using the shortcut name
    1. Position the cursor where you want to insert the XML snippet.
    2. Type < in the editor pane.
    3. Press ESC to close the IntelliSense complete word list.
    4. Type the shortcut name of the snippet, and press TAB to invoke the XML snippet.

【讨论】:

  • 我不明白为什么我给出了与 XML 智能感知有关的答案,因为问题涉及 C# 智能感知。 4 年前的我,你在想什么?
  • 不过,谢谢你,4 年前的你!你救了我!
  • @Oliver 这是我找到的关于 XML IntelliSense 和 Snippets 问题的唯一相关答案。谢谢!
【解决方案2】:

根据屏幕截图,您已经安装了 ReSharper,它会覆盖 VS 的 IntelliSense 行为。您可以关闭 Resharper 的覆盖,也可以直接在其中添加一个新的 LiveTemplate。更多细节在这里:

http://www.jetbrains.com/resharper/webhelp/Templates__Applying_Templates__Inserting_Imported_Code_Snippets.html

就我而言,我刚刚添加了一个新的 ReSharper 模板:

private $type$ _$loweredProperty$;

public $type$ $property$
{
    get { return _$loweredProperty$;}
    set 
    {
        if (_$loweredProperty$ == value) return;
        _$loweredProperty$ = value;
        OnPropertyChanged("$property$");
    }
}

而且效果更好:您只需输入两个单词 - 类型和属性名称。支持字段将以降低的首字母显示。您必须将“$loweredProperty$”设置为不可编辑的宏并将其指向 $property$。只需在模板编辑器中单击几下即可。

【讨论】:

  • 谢谢你帮了我很多。 Resharper 隐藏了 sn-p testm(它创建了一个新的测试方法)。但是现在我每次想使用 testm 时都必须输入 Ctrl + J。有没有办法将特定的 sn-ps 添加回智能感知?
【解决方案3】:

一秒钟没意识到,但这很简单:你最后错过了&lt;/CodeSnippets&gt;

【讨论】:

  • 我认为这可能是复制粘贴错误。我对几个代码 sn-ps 有同样的问题,而尾随标签不是原因。
  • 我在我的回答中结束了测试。它不适用于您在上面输入的文本文本,并且经过我的更正,它运行良好,所以它似乎确实是问题所在。
  • 我在片段编辑器中制作的sn-ps也有同样的问题,结束标签在那里,肯定还有另一个问题
  • @grant - 尝试提出一个新问题。如前所述,我的回答确实解决了这种情况下的问题。
【解决方案4】:

CTRL +K+ X

或 右键单击代码页将显示 sn-p

右键单击并在 Visualstudio 中启动智能感知

【讨论】:

    【解决方案5】:

    进入Visual Studio的Extensions and Updates,然后点击Online选项卡,然后在搜索中输入Bootstrap

    安装以下包以启用智能感知

    1. 引导包
    2. 引导片段包

    【讨论】:

      【解决方案6】:

      如果是 XML 语言的 sn-ps 则必须放在下一个目录

      C:\Users\%user%\Documents\Visual Studio 2015\Code Snippets\XML\My Xml Snippets\

      要将其中一个添加到您的文档中,您必须调用 sn-p 上下文菜单 Ctrl+K,Ctrl+X。 您的 sn-ps 将位于 “我的 Xml 片段”

      【讨论】:

        【解决方案7】:

        这可能来得有点晚,但是如果您正在调试程序,CTRL K + CTRL X 将不起作用。停止调试您的程序并重试。它在 VS 2013 中对我有用,并且没有 Resharper。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-12-16
          • 2015-12-24
          • 2015-12-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-09
          相关资源
          最近更新 更多