Skip to content

Instantly share code, notes, and snippets.

@ShuhuaGao
Created June 28, 2021 03:19
Show Gist options
  • Select an option

  • Save ShuhuaGao/bff38344143717ace1a468c78efcf338 to your computer and use it in GitHub Desktop.

Select an option

Save ShuhuaGao/bff38344143717ace1a468c78efcf338 to your computer and use it in GitHub Desktop.
Property snippet with change notification in Stylet MVVM.
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>props</Title>
<Shortcut>props</Shortcut>
<Description>Code snippet for property and backing field and notify with Stylet</Description>
<Author>Gao Shuhua</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<ToolTip>Property type</ToolTip>
<Default>int</Default>
</Literal>
<Literal>
<ID>property</ID>
<ToolTip>Property name</ToolTip>
<Default>MyProperty</Default>
</Literal>
<Literal>
<ID>field</ID>
<ToolTip>The variable backing this property</ToolTip>
<Default>myVar</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[private $type$ $field$;
public $type$ $property$
{
get => $field$;
set => SetAndNotify(ref $field$, value);
}
$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
@ShuhuaGao
Copy link
Author

ShuhuaGao commented Jun 28, 2021

HOW TO USE

  • In Visual Studio, go to Tools -- Code Snippets Manager, choose Language to CSharp, then
    • Add a new (empty) folder for your custom code snippets (if you have not done it yet)
    • Import the downloaded props.snippet to the above location. (Can also copy the props.snippet manually to the above folder.)

image

  • Stylet is an excellent lightweight MVVM framework for WPF. The class that needs such a notifiable property should inherit at least Stylet.PropertyChangedBase that defines the SetAndNotify method.
  • In writing code, simply type "props" and tab twice to use the snippet. A generated example is pasted below
private string title;

public string Title
{
    get => title;
    set => SetAndNotify(ref title, value);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment