Was*_*RAR 8 c# inotifypropertychanged visual-studio code-snippets
我找到了INotifyPropertyChanged的这段代码片段
但是它显示了这样的代码:

我会这样的:
对于公众:第一个字母的大写字母+ ...
私人:第一个字母+下划线+小写字母+ ...
我怎样才能做到这一点?
编辑:无需键入公共字段和私有字段
<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方法适用于大型项目.
我不认为这可以通过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()为您生成方法.
| 归档时间: |
|
| 查看次数: |
9434 次 |
| 最近记录: |