Skip to main content

Snippet Visual Studio pour propriété notifiant avec INotifyPropertyChanged

image Si comme moi vous écrivez des applications Silverlight ou WPF avec le modèle MVVM, vous en avez sûrement assez d’écrire le code (verbeux) d’une propriété notifiant de ses changements.

Voici un snippet Visual Studio permettant de vous simplifier la vie. Placez-le dans un fichier MonSnippet.snippet, allez dans le menu Outils/Gestionnaire des extraits de code et cliquez sur le bouton Importer. Sélectionnez le fichier MonSnippet.snippet que vous venez de créer et c’est gagné. Pour l’utiliser vous n’avez plus qu’à taper propnotif dans votre code et appuyer sur la touche Tab.

Code à mettre dans lefichier MonSnippet.snippet:

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>
        Notifier property.
      </Title>
    <Shortcut>propnotif</Shortcut>

    </Header>
    <Snippet>

      <Declarations>
        <Literal>
          <ID>PropertyType</ID>
          <Default>string</Default>
        </Literal>
        <Literal>
          <ID>PropertyName</ID>
          <Default>MyProperty</Default>
        </Literal>
        <Literal>
          <ID>FieldName</ID>
          <Default>myProperty</Default>
        </Literal>
      </Declarations>
      <Code Language="CSharp">
        <![CDATA[
                    $PropertyType$ $FieldName$;
                    public $PropertyType$ $PropertyName$
                    {
                        get
                        {
                            return $FieldName$;
                        }
                        set
                        {
                            $FieldName$ = value;
                            base.RaisePropertyChanged("$PropertyName$");
                        }
                    }
                ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Ce snippet suppose bien sûr que vous héritez d’une classe ayant une méthode RaisePropertyChanged(string propertyName).

Comments

Popular posts from this blog

Learn Meteor book available

I'm pleased to announce the general release of my Learn Meteor book. It is now available as an ebook or print book from various sources: Learn Meteor print (paperback) on Lulu Learn Meteor ebook on LeanPub Learn Meteor ebook on Barnes & Noble Learn Meteor ebook on iBooks Learn Meteor ebook on Kobo Learn Meteor ebook on Scribd Learn Meteor ebook on Inktera Page Foundry Learn Meteor ebook on 24symbols Learn Meteor ebook on Amazon US Learn Meteor ebook on Amazon UK Learn Meteor ebook on Amazon France Learn Meteor ebook on Amazon Deutschland Learn Meteor ebook on Amazon Canada Learn Meteor ebook on Amazon India Learn Meteor ebook on Amazon Brasil Learn Meteor ebook on Amazon Mexico Learn Meteor ebook on Amazon España Learn Meteor ebook on Amazon Italia Learn Meteor ebook on Amazon Netherlands Learn Meteor ebook on Amazon Japan Learn Meteor ebook on Amazon Australia More sources are coming soon for the print version. Learn Meteor has been a fun experienc