snippets can be written in xml and can be made for any language in 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>
this is the code to automatically generate the notify property changed method . all you need to do is to save this in a single file with the extension as snippet in your Documents/VisulaStudio(YourVersion)/Code Snippets/Visual C#/
that's it you are ready to use it ...
Now,observe the code snippet there is shortcut tag .. this tag references the tag that you should use in vs while writing to activate the snippet ..
here is the code for the property itself:
<?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>
in this particular snippet all you need to do is type nprop and press tab tab it generates the requied code .. you only need to enter the datatype and the name .. the rest is taken care of by the snippet itself ...
Though this i a better solution and greatly improves the coding speed, this is suitable for small projects only the viewmodelbase method is suitable for larger projects.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…