VS 2019自定义代码片段,其文字无法正确扩展

问题描述

我写了很多配置代码。我构建了此代码片段,以使所有重复代码都可以更快地编写。但是它无法正确展开。

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>New Configuration Property</Title>
            <Shortcut>newproperty</Shortcut>
        </Header>
        <Snippet>
            <Imports>
                <Import>
                    <Namespace>System.Configuration</Namespace>
                </Import>
            </Imports>
            <Declarations>
                <Literal>
                    <ID>$ConfigName$</ID>
                    <ToolTip>The name of the element property in the .config file</ToolTip>
                    <Default>configName</Default>
                </Literal>
                <Literal>
                    <ID>$PropertyName$</ID>
                    <ToolTip>The name of the class property</ToolTip>
                    <Default>PropertyName</Default>
                </Literal>
            </Declarations>
            <Code Language="CSharp" kind="" delimiter="$">
                <![CDATA[[ConfigurationProperty("$ConfigName$",Isrequired = true)]
public string $PropertyName$
{
    get { return (string)this["$ConfigName$"]; }
    set { this["$ConfigName$"] = value; }
}$selected$$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

这就是输出的样子。

        [ConfigurationProperty("",Isrequired = true)]
        public string 
{
    get { return (string) this[""]; }
        set { this[""] = value; }
}

没有提示输入文字。有什么建议吗?

解决方法

只需通过以下方式使其变得简单容易

  <?xml version="1.0" encoding="utf-8"?>
    <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
      <CodeSnippet Format="1.0.0">
        <Header>
          <Title>New Configuration Property</Title>
          <Shortcut>newproperty</Shortcut>
        </Header>
        <Snippet>
          <Imports>
            <Import>
              <Namespace>System.Configuration</Namespace>
            </Import>
          </Imports>
          <Declarations>
            <Literal>
              <ID>ConfigName</ID>
              <ToolTip>The name of the element property in the .config file</ToolTip>
              <Default>configName</Default>
            </Literal>
            <Literal>
              <ID>PropertyName</ID>
              <ToolTip>The name of the class property</ToolTip>
              <Default>PropertyName</Default>
            </Literal>
          </Declarations>
          <Code Language="csharp">
            <![CDATA[[ConfigurationProperty("$ConfigName$",IsRequired = true)]
    public string $PropertyName$
    {
        get { return (string)this["$ConfigName$"]; }
        set { this["$ConfigName$"] = value; }
    }
   $selected$ $end$]]>
          </Code>
        </Snippet>
      </CodeSnippet>
    </CodeSnippets>

您不需要这两个部分= kind =“” delimiter =“ $”并且您不需要在ID内包含美元符号,而不必这样做:$ ConfigName $您可以这样做:ConfigName只需看上面的xml代码段,如果您写的小写字母如:csharp而不是Csharp或CSharp,则该语言应该是不错的选择。希望它能对您有所帮助,