使用INotifyPropertyChanged的代码片段

Was*_*RAR 8 c# inotifypropertychanged visual-studio code-snippets

我找到了INotifyPropertyChanged的这段代码片段

但是它显示了这样的代码:

INotifyPropertyChanged的

我会这样的:

  1. 对于公众:第一个字母的大写字母+ ...

  2. 私人:第一个字母+下划线+小写字母+ ...

我怎样才能做到这一点?

编辑:无需键入公共字段和私有字段

<Snippet>
    <Declarations>
        <Literal>
            <ID>type</ID>
            <ToolTip>Property type</ToolTip>
            <Default>string</Default>
        </Literal>
        <Literal>
            <ID>property</ID>
            <ToolTip>Property name</ToolTip>
            <Default>MyProperty</Default>
        </Literal>
        <Literal>
            <ID>notifyMethod</ID>
            <ToolTip>name of method to raise PropertyChanged event</ToolTip>
            <Default>NotifyPropertyChanged</Default>
        </Literal>
    </Declarations>
    <Code Language="csharp">
        <![CDATA[private $type$ _$property$;
            public $type$ $property$
            {
                get { return _$property$;}
                set 
                { 
                    if (value != _$property$)
                    {
                        _$property$ = value;
                        $notifyMethod$("$property$");
                    }
                }
            }
        $end$]]>
    </Code>
</Snippet>
Run Code Online (Sandbox Code Playgroud)

Aka*_*tha 14

片段可以用xml编写,可以用于vs中的任何语言

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>Notify Property Changed Method</Title>
    <Author>Akash</Author>
    <Shortcut>npcm</Shortcut>
    <Description>This method implements the OnPropertyChanged method and binds to the event handler</Description>
    <SnippetTypes>
      <SnippetType>SurroundsWith</SnippetType>
      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>

    <Code Language="CSharp">
      <![CDATA[#region Notify Property Changed Members
  public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if(handler!=null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }    
        #endregion]]>
    </Code>
  </Snippet>
</CodeSnippet>
Run Code Online (Sandbox Code Playgroud)

这是自动生成notify属性更改方法的代码.您需要做的就是将它保存在一个文件中,扩展名为您的Documents/VisulaStudio(YourVersion)/ Code Snippets/Visual C#/中的片段

那就是你准备好使用它......

现在,观察代码片段中有快捷方式标记..此标记引用了在写入时应该在vs中使用的标记以激活代码片段.

这是属性本身的代码:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>Notifiable Property</Title>
    <Author>Akash</Author>
    <Shortcut>nprop</Shortcut>
    <Description>Property With in Built Property Changed method implementation.</Description>
    <SnippetTypes>
      <SnippetType>SurroundsWith</SnippetType>
      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal>
        <ID>Type</ID>
        <Default>string</Default>
      </Literal>
      <Literal>
        <ID>Property</ID>
        <Default>PlaceHolder</Default>
      </Literal>
    </Declarations>
    <Code Language="CSharp">
      <![CDATA[private $Type$ _$Property$;
        public $Type$ $Property$
        {
            get { return _$Property$; }
            set { 
               if(value!=null || value != _$Property$) _$Property$ = value;
               OnPropertyChanged("$Property$");
            }
        }]]>
    </Code>
  </Snippet>
</CodeSnippet>
Run Code Online (Sandbox Code Playgroud)

在这个特定的片段中你需要做的就是键入nprop并按tab tab它会生成所需的代码..你只需要输入数据类型和名称..其余部分由片段本身处理......

虽然这是一个更好的解决方案并且大大提高了编码速度,但这适用于小型项目,只有viewmodelbase方法适用于大型项目.

  • 多年以后但绝对是最好的答案.不需要Resharper (3认同)

Gil*_*dor 7

我不认为这可以通过Visual Studio提供的本机代码片段功能来完成.

我个人使用Resharper,这使它成为可能.它可以把我写的代码变成像

public string Name { get; set; }
Run Code Online (Sandbox Code Playgroud)

private string _name;
public string Name
{
    get { return _name; }
    set
    {
        if(value == _name)
            return;
        _name = value;
        OnPropertyChanged("Name");
    }
}
Run Code Online (Sandbox Code Playgroud)

它甚至OnPropertyChanged()为您生成方法.

  • @Schneider这个特性是在Resharper 7中引入的.只需将光标放在实现`INotifyPropertyChanged`的类的属性上,就应该看到上下文操作菜单.有关详细信息,请参阅[此处](http://blogs.jetbrains.com/dotnet/2012/07/inotifypropertychanged-support-in-resharper-7/). (2认同)