【问题标题】:Parse Replacement Literal in Visual Studio Snippet [duplicate]在 Visual Studio 代码段中解析替换文字 [重复]
【发布时间】:2012-11-28 00:48:58
【问题描述】:

可能重复:
Formatting Literal parameters of a C# code snippet

编辑:这可以关闭。找到一个完全相同的副本,似乎没有解决方案。 =(

完全重复: Formatting Literal parameters of a C# code snippet

在编写 sn-p 时有什么方法可以解析替换文字吗?我想做类似以下的事情:

<Literal>
    <ID>PropertyName</ID>
</Literal>

用户将 PropertyName 替换为“MyProperty”,结果如下:

private object _myProperty;

public object MyProperty
{get;set;}

注意大写。我需要一种方法来解析替换文字并对其进行操作。下划线是微不足道的,只是硬编码的问题。

这里有机会吗?

编辑;完整的sn-p:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>MVVM Public Property</Title>
      <Author>Michael Leide</Author>
      <Description>Adds a public property with private backing and property changed event support.</Description>
      <Shortcut>propvm</Shortcut>
    </Header>

    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>PropertyType</ID>
          <Default>object</Default>          
        </Literal>

        <Literal Editable="true">
          <ID>PropertyName</ID>
          <Default>PropertyName</Default>
        </Literal>
      </Declarations>

      <Code Language="csharp" Kind="" Delimiter="$">
        <![CDATA[
        $PropertyType$ _$PropertyName$;

        public $PropertyType$ $PropertyName$ {
            get {
                if (_$PropertyName$ == null)
                    _$PropertyName$ = new $PropertyType$();
                return _$PropertyName$;
            } set {
                _$PropertyName$ = value;
                this.OnPropertyChanged("$PropertyName$");
        }   }
        ]]>      
      </Code>

    </Snippet>
  </CodeSnippet>
</CodeSnippets>

【问题讨论】:

  • 你能发布你的完整sn-p吗?
  • 你明白了。如最初所述,我想将 PropertyName 转换为 _propertyName。

标签: c# xml visual-studio code-snippets


【解决方案1】:

很遗憾,您不能直接将您的 PropertyName 转换为 propertyName。解决此问题的最佳方法是包含第三个文字来指定支持字段。这可以防止您当前文字中的歧义,并且只会增加额外的几次击键。

声明块如下所示:

  <Declarations>
    <Literal Editable="true">
      <ID>PropertyType</ID>
      <Default>object</Default>          
    </Literal>

    <Literal Editable="true">
      <ID>PropertyName</ID>
      <Default>PropertyName</Default>
    </Literal>

    <Literal Editable="true">
      <ID>BackingPropertyField</ID>
      <Default>backingPropertyField</Default>
    </Literal>
  </Declarations>

而代码块变成:

    public $PropertyType$ $PropertyName$ {
        get {
            if (_$BackingPropertyField$ == null)
                _$BackingPropertyField$ = new $PropertyType$();
            return _$BackingPropertyField$;
        } set {
            _$BackingPropertyField$ = value;
            this.OnPropertyChanged("$PropertyName$");
    }   }
    ]]>      
  </Code>

然后,您可以通过 Tab 键浏览并指定所需的名称。

【讨论】:

  • 看来你是绝对正确的,虽然这很遗憾。 sn-p“要求”的一部分是它将同步支持字段和属性名称。从最纯粹的角度来看,这可能是模棱两可的,但 sn-p 只适合我,我很清楚发生了什么。我想我的支持字段只需要在下划线后面加一个大写字母 =)
猜你喜欢
  • 2012-05-13
  • 1970-01-01
  • 2018-07-07
  • 1970-01-01
  • 2012-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多