Lo de crear Code Snippets (Fragentos de Código) es una de esas cosas que siempre he dejado para aprender más tarde y al final nunca he aprendido... hasta hoy, que tenía que hacer un objeto con más de treinta propiedades que lanzasen el PropertyChanged y no he podido retrasarlo más.
Para aumentar mi productividad he perdido un poco el tiempo y me he creado un Code Snipplet para las propiedades que implementan el INotifyPropertyChanged, lo he llamado propNot
Para ello, en Visual Studio me fui a Tools y desde allí al Code Snippets Manager. Una vez allí seleccioné C# y luego copié
Me situé en esa carpeta, creé un fichero .snippet y escribí esto:
- <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
- <CodeSnippet Format="1.0.0">
- <Header>
- <Title>propNot</Title>
- <Shortcut>propNot</Shortcut>
- <Description>Code snippet for properties with NotifyPropertyChanged</Description>
- <Author>Chan</Author>
- <SnippetTypes>
- <SnippetType>Expansion</SnippetType>
- <SnippetType>SurroundsWith</SnippetType>
- </SnippetTypes>
- </Header>
- <Snippet>
- <Declarations>
- <Literal>
- <ID>privName</ID>
- <Default>privName</Default>
- <ToolTip>Private name of the property</ToolTip>
- </Literal>
- <Literal>
- <ID>pubName</ID>
- <Default>pubName</Default>
- <ToolTip>Public name of the property</ToolTip>
- </Literal>
- <Literal>
- <ID>type</ID>
- <Default>string</Default>
- <ToolTip>Type of the property</ToolTip>
- </Literal>
- </Declarations>
- <Code Language="csharp"><![CDATA[private $type$ $privName$;
- public $type$ $pubName$
- {
- get { return $privName$; }
- set
- {
- $privName$ = value;
- // Call NotifyPropertyChanged when the property is updated
- NotifyPropertyChanged("$pubName$");
- }
- }]]>
- </Code>
- </Snippet>
- </CodeSnippet>
- </CodeSnippets>
Probado y funcionando, tanto el binding TwoWay como los Code Snippets. Ahora que sé cómo se hace lo voy a hacer muy a menudo. --EDITO-- Mentí, no los hago a menudo porque soy MUY flojo, pero sí que uso mucho este otro snippet:
- <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
- <CodeSnippet Format="1.0.0">
- <Header>
- <Title>INotify region with the necessary methods to implement the interface</Title>
- <Shortcut>inotifyregion</Shortcut>
- <Description>Creates region with the necessary methods to implement the interface INotifyPropertyChanged</Description>
- <Author>Jose Sanchez</Author>
- <SnippetTypes>
- <SnippetType>Expansion</SnippetType>
- <SnippetType>SurroundsWith</SnippetType>
- </SnippetTypes>
- </Header>
- <Snippet>
- <Declarations />
- <Code Language="csharp"><![CDATA[#region INotifyMethods
- // Declare the event
- public event PropertyChangedEventHandler PropertyChanged;
- // Create the OnPropertyChanged method to raise the event
- protected void NotifyPropertyChanged(string name)
- {
- PropertyChangedEventHandler handler = PropertyChanged;
- if (handler != null)
- {
- handler(this, new PropertyChangedEventArgs(name));
- }
- }
- #endregion]]>
- </Code>
- </Snippet>
- </CodeSnippet>
- </CodeSnippets>
No hay comentarios:
Publicar un comentario