带有文字的 VS 2019 自定义代码片段未正确扩展

VS 2019 custom code snippet with literals not expanding correctly

我写了很多配置代码。我构建这个片段是为了让所有重复的代码编写起来更快。但是它没有正确展开。

<?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。希望对你有所帮助,