VS2010代码段快捷方式未显示

13

我在VS2010中创建了一个代码片段,但在我开始输入时没有显示为快捷方式。它被命名为propnch。

当我使用Ctrl-K、Ctrk-X时可以看到它,但是当我只是输入prop...时,它不会显示为选项。

我是否错过了某种设置?

我有截图,但我认为SO不允许上传任何东西。

编辑:屏幕截图

我可以使用Ctrl-K、Ctrl-X看到我的代码片段(当我使用ctrl-PrtScn截图时,它变成了灰色)

enter image description here

但它不会出现在其他代码片段的快捷方式中。

enter image description here

代码片段在此处(取自这篇教程),位于“文档\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>


您可以通过在编辑框上方点击图片按钮来添加屏幕截图。 - JaredPar
你能发一下你的.snippet文件吗?我已经成功地复制了一个名为 propnch 的代码片段,并且它工作正常。 - p.campbell
嗨,我已经添加了屏幕截图和代码片段。 - big_tommy_7bb
7个回答

7

原来这是VS2010中xml编辑器的设计缺陷。在C#编辑器中,只需键入快捷键并按“tab”键即可完成。但在xml编辑器中,需要再按两个按键。

引用文档:

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#智能感知。四年前的我,你当时在想什么? - Oliver
不过,还是要感谢四年前的你!你救了我! - Varvara Kalinina
@Oliver 这是我找到的唯一与我的问题有关的XML IntelliSense和Snippets的相关答案,谢谢! - Attila Antal

4
根据截图显示,您已安装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隐藏了代码片段testm(用于创建新的测试方法)。然而现在每次想要使用testm时都需要键入Ctrl + J。是否有一种方法可以将特定的代码片段添加回智能感知中? - buddybubble

0
花了一点时间才意识到,但很简单:你在结尾处缺少了</CodeSnippets>

我认为这可能是复制粘贴错误。我有几个代码片段遇到了同样的问题,但结尾标签不是原因。 - Randy Eppinger
我用我的答案中的结尾进行了测试。它与您输入的文本不起作用,但是在我进行更正后,它可以正常工作,因此似乎确实是问题所在。 - zanlok
我在使用片段编辑器创建片段时遇到了同样的问题,结束标签已经存在,可能还有其他问题。 - Grant Crofton
@grant - 尝试提出一个新问题。正如所示,我的答案确实解决了这个问题。 - zanlok

0

CTRL + K + X

或者右键单击代码页面将显示片段

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


0
在Visual Studio的扩展和更新中,点击在线选项卡,然后在搜索框中输入Bootstrap
安装以下包以启用智能感知:
  1. Bootstrap Bundle
  2. Bootstrap Snippet Pack

0
这可能有点晚了,但如果您正在调试程序,CTRL K + CTRL X将不起作用。停止调试程序,然后再次尝试。在VS 2013中,并没有使用Resharper,它对我有效。

0
如果这是XML语言的片段,它必须放置在下一个目录中:
C:\Users\%user%\Documents\Visual Studio 2015\Code Snippets\XML\My Xml Snippets\
要将其中之一添加到您的文档中,您必须通过ctrl+K、ctrl+X调用片段上下文菜单。 您的片段将在"My Xml Snippets"中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接