Codesnippet for RelayCommand
February 4, 2011 4 Comments
If you are working with MVVMLight then the RelayCommand provides binding from the View to commands within the ViewModel. To register a command it requires a couple lines of code. The perfect opportunity to write a code snippet.
Here the snippets I was missing from MVVMLight:
Codesnippet for RelayCommand
1: <?xml version="1.0" encoding="utf-8" ?>
2: <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
3: <CodeSnippet Format="1.0.0">
4: <Header>
5: <Title>RelayCommand</Title>
6: <Shortcut>relaycmd</Shortcut>
7: <Description>Define a RelayCommand</Description>
8: <Author>Jan Hannemann</Author>
9: <SnippetTypes>
10: <SnippetType>Expansion</SnippetType>
11: </SnippetTypes>
12: </Header>
13: <Snippet>
14: <Declarations>
15: <Literal>
16: <ID>CMD</ID>
17: <ToolTip>Command Property</ToolTip>
18: <Default>Command</Default>
19: </Literal>
20: <Literal>
21: <ID>CMDF</ID>
22: <ToolTip>Command Field</ToolTip>
23: <Default>command</Default>
24: </Literal>
25: </Declarations>
26: <Code Language="csharp">
27: <![CDATA[
28: private RelayCommand _$CMDF$;
29: public RelayCommand $CMD$
30: {
31: get
32: {
33: return _$CMDF$;
34: }
35: }
36:
37: /// <summary>
38: /// Checks whether the $CMD$ command is executable
39: /// </summary>
40: private bool Can$CMD$()
41: {
42: return true;
43: }
44:
45: /// <summary>
46: /// Executes the $CMD$ command
47: /// </summary>
48: private void Exec$CMD$()
49: {
50: throw new NotImplementedException();
51: }
52:
53: //cut and paste this line to your ctor
54: _$CMDF$ = new RelayCommand(Exec$CMD$, Can$CMD$);
55: $end$]]>
56: </Code>
57: </Snippet>
58: </CodeSnippet>
59: </CodeSnippets>
Codesnippet for RelayCommand<T>
1: <?xml version="1.0" encoding="utf-8" ?>
2: <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
3: <CodeSnippet Format="1.0.0">
4: <Header>
5: <Title>RelayCommandGeneric</Title>
6: <Shortcut>relaycmdg</Shortcut>
7: <Description>Define a generic RelayCommand</Description>
8: <Author>Jan Hannemann</Author>
9: <SnippetTypes>
10: <SnippetType>Expansion</SnippetType>
11: </SnippetTypes>
12: </Header>
13: <Snippet>
14: <Declarations>
15: <Literal>
16: <ID>CMD</ID>
17: <ToolTip>Command Property</ToolTip>
18: <Default>Command</Default>
19: </Literal>
20: <Literal>
21: <ID>CMDF</ID>
22: <ToolTip>Command Field</ToolTip>
23: <Default>command</Default>
24: </Literal>
25: <Literal>
26: <ID>GT</ID>
27: <ToolTip>Generic Type</ToolTip>
28: <Default>int</Default>
29: </Literal>
30: </Declarations>
31: <Code Language="csharp">
32: <![CDATA[
33: private RelayCommand<$GT$> _$CMDF$;
34: public RelayCommand<$GT$> $CMD$
35: {
36: get
37: {
38: return _$CMDF$;
39: }
40: }
41:
42: /// <summary>
43: /// Checks whether the $CMD$ command is executable
44: /// </summary>
45: private bool Can$CMD$($GT$ val)
46: {
47: return true;
48: }
49:
50: /// <summary>
51: /// Executes the $CMD$ command
52: /// </summary>
53: private void Exec$CMD$($GT$ val)
54: {
55: throw new NotImplementedException();
56: }
57:
58: //cut and paste this line to your ctor
59: _$CMDF$ = new RelayCommand<$GT$>(Exec$CMD$, Can$CMD$);
60: $end$]]>
61: </Code>
62: </Snippet>
63: </CodeSnippet>
64: </CodeSnippets>
You can download the snippets from here: MyCodeSnippets.rar