简体   繁体   English

在VS2013中为C#创建自定义代码段

[英]Creating Custom Code Snippet in VS2013 for C#

Creating Custom Code Snippet doesn't helped much to me. 创建自定义代码段对我没有多大帮助。 My question is specific to my requirement. 我的问题是针对我的要求的。

I want to write a custom snippet for my Property. 我想为我的媒体资源编写一个自定义代码段。 The situation is normally when we write prop and double tab we will get the output 通常情况是,当我们编写prop和double选项卡时,我们将获得输出

public int MyProperty { get; set; }

and when we write propfull we get 当我们写propfull时,我们得到

private int myVar;

public int MyProperty
{
    get { return myVar;}
    set { myVar = value;}
}

as soon as we change the variable name it automatically reflects everywhere 一旦我们更改了变量名,它就会自动反映在任何地方

Now I want to write my own snippet like this 现在我要这样写自己的代码段

public int MyProperty
{
    get
    {
        return GetValue(() => MyProperty);
    }
    set
    {
        SetValue(() => MyProperty, value);
    }
}

I have got Creating a Code Snippet from MSDN 我已经从MSDN获得了创建代码段的信息

This is what I have tried 这就是我尝试过的

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>propmy</Title>
        </Header>
        <Snippet>
            <Code Language="csharp"><![CDATA[public int MyProperty
        {
                get { return GetValue(() => MyProperty); }
                set { SetValue(() => MyProperty , value); }
        }
$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

But when I write propmy in VS IDE nothing show up in the list and it truns to prop in the first tab and on the second tab it create the property like normal one. 但是,当我在VS IDE中编写propmy时,列表中什么都没有显示,并且它会在第一个选项卡中显示prop,然后在第二个选项卡中创建属性,就像普通的一样。 I don't know how to proceed? 我不知道该如何进行?

You can try to add 您可以尝试添加

<Shortcut>propmy</Shortcut>

in the header section of the your XML. 在XML的标题部分。 I am sure this will do the trick 我相信这可以解决问题

Edit: 编辑:

I have created the complete XML for you. 我已经为您创建了完整的XML。 Just copy paste and it will gonna help you. 只需复制粘贴,它将对您有所帮助。

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>propmy</Title>
            <Shortcut>propmy</Shortcut>
            <Description>Automatically implemented property</Description>
            <Author>BugFree</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>
            </Declarations>
            <Code Language="csharp"><![CDATA[public $type$ $property$
        {
                get { return GetValue(() => $property$); }
                set { SetValue(() => $property$ , value); }
        }
$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM