C#+XAML:MVVM Light Toolkitの.net Framework4.5(C#5)以上用カスタムスニペット(Setメソッドを利用するプロパティ用)
前回の記事では.net4.5向けのmvvminpcスニペットを作成しましたが、 Setメソッド(プロパティ変更+変更通知を行うViewModelBaseのメソッド)を用いたプロパティ用スニペットについても.net4.5用のオーバーライドに対応したものがなさそうだったため、作り方を記載しようと思います。
1;以下の内容で「mvvmInpcSet45.snippet」というファイルを用意します。
<?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <SnippetTypes> <SnippetType>Expansion</SnippetType> </SnippetTypes> <Title>INPC "Set" Property</Title> <Shortcut>mvvminpcset45</Shortcut> </Header> <Snippet> <Declarations> <Literal Editable="true"> <ID>Type</ID> <ToolTip>Property type</ToolTip> <Default>bool</Default> <Function> </Function> </Literal> <Literal Editable="true"> <ID>AttributeName</ID> <ToolTip>Attribute name</ToolTip> <Default>_myProperty</Default> <Function> </Function> </Literal> <Literal Editable="true"> <ID>InitialValue</ID> <ToolTip>Initial value</ToolTip> <Default>false</Default> <Function> </Function> </Literal> <Literal Editable="true"> <ID>PropertyName</ID> <ToolTip>Property name</ToolTip> <Default>MyProperty</Default> <Function> </Function> </Literal> </Declarations> <Code Language="csharp"><![CDATA[private $Type$ $AttributeName$ = $InitialValue$; /// <summary> /// Sets and gets the $PropertyName$ property. /// Changes to that property's value raise the PropertyChanged event. /// </summary> public $Type$ $PropertyName$ { get { return $AttributeName$; } set { Set(ref $AttributeName$, value); } }]]></Code> </Snippet> </CodeSnippet> </CodeSnippets>
2:1で用意したファイルを「%UserProfile%¥Documents¥Visual Studio 2015¥Code Snippets¥Visual C#¥My Code Snippets」に保存します。 または、Visual Studinのメニュー「ツール > コード スニペット マネージャー」より、インポート機能でスニペットを追加します。
3:Visual StudioのC#ファイル内で「mvvmInpcSet45」と入力し、タブキーを押してみてください。(インテリセンスに表示されない場合は、Visual Studioを再起動してください)
上記の操作で、下記のような感じのスニペットが展開されたかと思います。
private bool _myProperty = false; /// <summary> /// Sets and gets the MyProperty property. /// Changes to that property's value raise the PropertyChanged event. /// </summary> public bool MyProperty { get { return _myProperty; } set { Set(ref _myProperty, value); } }
.net4.5以降向けのすっきりしたSetメソッド用スニペットとなりました。